Kamoulox
Kamoulox

Reputation: 351

JavaScript array of objects contains the same array data

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);

Upvotes: 7

Views: 3051

Answers (5)

tnkh
tnkh

Reputation: 1839

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)

Upvotes: 1

thelastworm
thelastworm

Reputation: 597

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());

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115222

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)

Upvotes: 3

Shrijan Tiwari
Shrijan Tiwari

Reputation: 693

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);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386610

You 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);

Upvotes: 12

Related Questions