0

I have an array like this:

var = [
    {
        "a": "value",
        "b": "value2"
    },
    {
        "a": "value3",
        "b": "value4"
    }
    ...
]

I need to find if any of the subarrays contain a certain value.

I tried

var.flat().includes("value")

but that always returned false for some reason and .flat() didn't even flatten the array.

I also tried

var.includes("value")

without the .flat() but that would only return if the top level includes it.

I could do

var = [
    "a": [
        "value",
        "value3"
        ...
    ],
    "b": [
        "value2",
        "value4"
        ...
    ]
]

but I'd rather not since that'd require me to rewrite some code I already wrote.

RedGuy11
  • 344
  • 6
  • 14
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – lbragile Dec 31 '20 at 05:35
  • 1
    @lbragile no, that will only return if the top level includes it – RedGuy11 Dec 31 '20 at 05:39

3 Answers3

3

Use flatMap to extract all nested values into a single flat array first:

const objs = [
    {
        "a": "value",
        "b": "value2"
    },
    {
        "a": "value3",
        "b": "value4"
    }
];
const values = objs.flatMap(Object.values);
console.log(values.includes("value"));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks! Could I have some clarification on how this works? Specifically, how is ```.flatMap()``` different from ```.flat()``` – RedGuy11 Dec 31 '20 at 05:23
  • 1
    @RedGuy11 See [**this**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) – Saadi Toumi Fouad Dec 31 '20 at 05:28
  • 1
    @RedGuy11 `Object.values` takes an object and extracts its values as an array. `flatMap` maps an array, then calls `.flat()` on the resulting mapped array. It's like `.map` followed by `.flat`. – CertainPerformance Dec 31 '20 at 05:31
  • @CertainPerformance ok, thanks. But dosen't ```.map()``` need a callback (that's what the link @SaymoinSam shared said) – RedGuy11 Dec 31 '20 at 05:35
  • 2
    @RedGuy11 `Object.values` is the callback. It gets called for every iteration and returns an array of values. Then `.flat` flattens the array of arrays of values. – CertainPerformance Dec 31 '20 at 05:36
  • @CertainPerformace ohhhhhhh I get it now tysm! – RedGuy11 Dec 31 '20 at 05:37
2

You could iterate with some (it would stop the loop immediately when encounter the first value), combine with Object.values (get array of values from an object) and .includes (to check if the array has value),

const data = [
  {
    a: "value",
    b: "value2",
  },
  {
    a: "value3",
    b: "value4",
  },
];

const valid = data.some((d) => Object.values(d).includes("value"));

console.log(valid);
hgb123
  • 13,869
  • 3
  • 20
  • 38
0

In your code, varr is already flatten array of objects. Since you want to check in Object values. Use varr.map(Object.values).flat()

varr = [
  {
    a: "value",
    b: "value2",
  },
  {
    a: "value3",
    b: "value4",
  },
];

console.log(varr.map(Object.values).flat().includes("value"));
Siva K V
  • 10,561
  • 2
  • 16
  • 29