最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript array of objects contains the same array data - Stack Overflow

programmeradmin1浏览0评论

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
  • So get all arrays elements that are present on all a? – Eddie Commented Mar 25, 2019 at 10:06
  • 3 Possible duplicate of Simplest code for array intersection in javascript – James Long Commented Mar 25, 2019 at 10:07
  • @JamesLong I don't understand the duplication, why this answer can help me ? – Kamoulox Commented Mar 25, 2019 at 10:08
  • @Eddie I need to get all array into the key data. This key is present into all object of my principal array. – Kamoulox Commented Mar 25, 2019 at 10:11
  • @Kamoulox you have 2 arrays (a[0].data and a[1].data). You seem to be wanting an array of all the values that appear in both arrays. Unless I'm mis-reading your question – James Long Commented Mar 25, 2019 at 10:12
 |  Show 3 more comments

5 Answers 5

Reset to default 12

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

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)

发布评论

评论列表(0)

  1. 暂无评论