I try to get all same data values into an array of objects. This is my input:
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
]
I need a result like:
["65d4ze"]
I try to loop on my object to get this output, but I'm completely lost... I don't know how to know if the result is into all data arrays.
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
],
b = [],
c = []
a.forEach(function(object) {
b.push(object.data.map(function(val) {
return val;
})
);
});
console.log(b);
I try to get all same data values into an array of objects. This is my input:
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
]
I need a result like:
["65d4ze"]
I try to loop on my object to get this output, but I'm completely lost... I don't know how to know if the result is into all data arrays.
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
],
b = [],
c = []
a.forEach(function(object) {
b.push(object.data.map(function(val) {
return val;
})
);
});
console.log(b);
Share
Improve this question
edited Mar 25, 2019 at 12:05
Peter Mortensen
31.6k22 gold badges110 silver badges133 bronze badges
asked Mar 25, 2019 at 10:04
KamouloxKamoulox
3511 gold badge3 silver badges13 bronze badges
8
|
Show 3 more comments
5 Answers
Reset to default 12You could map data
and get the common values with Array#map
, Array#reduce
, Array#filter
, Set
and Set#has
.
var array = [{ name: "Foo", id: "123", data: ["65d4ze", "65h8914d"] }, { name: "Bar", id: "321", data: ["65d4ze", "894ver81"] }],
key = 'data',
common = array
.map(o => o[key])
.reduce((a, b) => b.filter(Set.prototype.has, new Set(a)));
console.log(common);
You can use the Array#filter
method. Filter the first array by checking if a value is present in all other object properties (arrays), using the Array#every
method to check if a value is present in all remaining arrays.
let res = a[0].data.filter(v => a.slice(1).every(a => a.data.includes(v)));
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
];
let res = a[0].data.filter(v => a.slice(1).every(a => a.data.includes(v)));
console.log(res)
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
],
b = {};
a.forEach(function(i) {
i.data.forEach(function(j) {
if (!b.hasOwnProperty(j)) {
b[j] = 0;
}
b[j] = b[j] + 1;
});
});
c = []
for (var i in b) {
if (b.hasOwnProperty(i)) {
if (b[i] > 1) {
c.push(i)
}
}
}
console.log(c);
Use the flat
function in the array:
var a = [{
name: "Foo",
id: "123",
data: ["65d4ze", "65h8914d"]
},
{
name: "Bar",
id: "321",
data: ["65d4ze", "894ver81"]
}
],
b = [],
c = []
a.forEach(function(object) {
b.push(object.data.map(function(val) {
return val;
})
);
});
console.log(b.flat());
You could use reduce
and concat
on each data array, and check the count of each item.
In the end, you check whether all objects across the array contain that item and return it if yes.
Note that this function works if you want to extract the item that has the same occurrence across all objects in the array.
If an item has duplicates, but does not fulfill the above condition, it would not be extracted.
let a = [{name: "Foo",id: "123",data: ["65d4ze", "65h8914d"]},{name: "Bar",id: "321",data: ["65d4ze", "894ver81"]}]
let arr = a.reduce((prev,next) => prev.data.concat(next.data))
let counts = {};
let result = [];
for (var i = 0; i < arr.length; i++) {
var num = arr[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
for (let i in counts) {
if (counts[i] === a.length) {
result.push(i)
}
}
console.log(result)
a
? – Eddie Commented Mar 25, 2019 at 10:06