Getting name from nesting arrays from matching arrays
I have multiple arrays, and if they contain the same values, I would like to return the name of these arrays.
var x = { "food": ['bacon', 'cheese', 'bread', 'tomato'], "utilities": ['plates', 'forks', 'spatulas'], "guests": ['john', 'matt', 'bill'] }, y = ['bacon', 'tomato', 'forks'];
I have a variable x
and it has multiple arrays named either food
, or utilities
, or guests
. All it contains y
is some values ββthat are the same in some of these arrays in a variable x
. I need to return the name of the array, containing bacon
, tomato
and forks
in their arrays. Therefore, for this example, I need to return: ["food", "utilities"]
.
function getContainerName (obj, values) { return Object.keys (obj) .find (function (key) { return values.every (value => obj [key] .find (function (elem) { return elem === value; })); }); } console.log (getContainerName (x, y));
When you throw them through this function, I get an error *********
. How can I go and get the returned array ["food", "utilities"]
?
source to share
Simple reduce()
to Object.keys
complete the task
var x = {
"food": ['bacon', 'cheese', 'bread', 'tomato'],
"utilities": ['plates', 'forks', 'spatulas'],
"guests": ['john', 'matt', 'bill']
},
y = ['bacon', 'tomato', 'forks'];
let res = Object.keys(x).reduce((a, b) => {
if (x[b].some(v => y.includes(v))) a.push(b);
return a;
}, []);
console.log(res);
For your comment - with var and a normal function:
var res = Object.keys(x).reduce(function (a, b) {
if (x[b].some(function (v) {
return y.indexOf(v) !== -1;
})) a.push(b);
return a;
}, []);
source to share
You can Array # filter an array of Key # object using an array map y
and Array # some .
var x = {
"food": ['bacon', 'cheese', 'bread', 'tomato'],
"utilities": ['plates', 'forks', 'spatulas'],
"guests": ['john', 'matt', 'bill']
};
var y = ['bacon', 'tomato', 'forks'];
var yMap = y.reduce(function(o, v) { // create a map to save iteration of y on each comparsion
o[v] = true;
return o;
}, {});
var result = Object.keys(x).filter(function(key) { // filter the keys
return x[key].some(function(v) { // keep the key, if at least one item of the array is in the map
return yMap[v];
});
});
console.log(result);
And ES6 version using Set :
const x = {
"food": ['bacon', 'cheese', 'bread', 'tomato'],
"utilities": ['plates', 'forks', 'spatulas'],
"guests": ['john', 'matt', 'bill']
};
const y = ['bacon', 'tomato', 'forks'];
const ySet = new Set(y);
const result = Object.keys(x).filter((key) => x[key].some((item) => ySet.has(item)));
console.log(result);
source to share
You can do this using . filter () and . some () methods with : Object.keys()
function getContainerName(obj, values) {
return Object.keys(obj).filter(function(key) {
return y.some(function(v){
return obj[key].indexOf(v) !== -1;
})
});
}
Demo:
var x = {
"food": ['bacon', 'cheese', 'bread', 'tomato'],
"utilities": ['plates', 'forks', 'spatulas'],
"guests": ['john', 'matt', 'bill']
},
y = ['bacon', 'tomato', 'forks'];
function getContainerName(obj, values) {
return Object.keys(obj).filter(function(key) {
return y.some(function(v){
return obj[key].indexOf(v) !== -1;
})
});
}
console.log(getContainerName(x, y));
source to share