Ad

Javascript How Do I Dynamically Find Keys In My Array?

- 1 answer

I'm trying to find all the keys that are in my array to figure out if some keys already exist in there. Tho when I try to put in the keys seperate like this:

console.log(this.UrlArray.find(({color_ids}) => color_ids));

It works, but if I try doing it with the variable with the actual keys in it:

console.log(this.UrlArray.find(({prefixEqual}) => prefixEqual));

It doesn't work. The values that belong to the keys aren't important, but I stil can't figure out how to do it.

Array example:

(3) [{…}, {…}, {…}]
  0:
    bd_shoe_size_ids: Array(2)
      0: "6601"
      1: "6598"
    length: 2
    [[Prototype]]: Array(0)
    [[Prototype]]: Object
  1:
    color_ids: Array(2)
      0: "6056"
      1: "6044"
    length: 2
    [[Prototype]]: Array(0)
    [[Prototype]]: Object
  2:
    manufacturer_ids: Array(2)
      0: "5875"
      1: "5866"
    length: 2
    [[Prototype]]: Array(0)
    [[Prototype]]: Object
  length: 3
[[Prototype]]: Array(0)

I asked something like this before, but I got linkes to answers that had nothing to do with what I'm trying to do and it closed.

As desired outputs I just wanna see bd_shoe_size_ids, color_ids and manufacturer_ids when I console log it.

Ad

Answer

const data = [
    { bd_shoe_size_ids: [1, 2] },
    { color_ids: [3, 4] },
    { manufacturer_ids: [5, 6] },
];

const output = data.map(Object.keys).flat();

console.log(output);

Ad
source: stackoverflow.com
Ad