0

I need to find value of type2.id where id is 7 in the following object

[
  {
    "type1": {
      "id": "1",
      "value": "val1"
    },
    "type2": [
      {
        "id": "2",
        "value": "val2"
      }
    ]
  },
  {
    "type1": null,
    "type2": [
      {
        "id": "5",
        "value": "val5"
      }
    ]
  },
  {
    "type1": {
      "id": "3",
      "value": "val3"
    },
    "type2": [

    ]
  },
  {
    "type1": {
      "id": "4",
      "value": "val4"
    },
    "type2": [
      {
        "id": "7",
        "value": "val7"
      }
    ]
  }
]

Please notice type1 is a simple object and type 2 is an array here, there can be empty array in type2 as well.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Also you do not have "a json" you have a plain old javascript object – mplungjan Jan 26 '23 at 07:57
  • 3
    Does this answer your question? [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – derpirscher Jan 26 '23 at 08:00

5 Answers5

0

let value = null;

// First way using forEach
obj.forEach(item => {
  if (item.type2 && item.type2.length > 0) {
    item.type2.forEach(type2 => {
      if (type2.id === '7') {
        value = type2.value;
      }
    });
  }
});

console.log("first way : ", value);


// Second way using map and filter
let type2Array = obj.map(obj => obj.type2).flat()
value = type2Array.filter(obj => obj.id === '7').at(0)?.value

console.log("second way : ", value);
<script>
  const obj = [{
      "type1": {
        "id": "1",
        "value": "val1"
      },
      "type2": [{
        "id": "2",
        "value": "val2"
      }]
    },
    {
      "type1": null,
      "type2": [{
        "id": "5",
        "value": "val5"
      }]
    },
    {
      "type1": {
        "id": "3",
        "value": "val3"
      },
      "type2": [

      ]
    },
    {
      "type1": {
        "id": "4",
        "value": "val4"
      },
      "type2": [{
        "id": "7",
        "value": "val7"
      }]
    }
  ];
</script>
Lionel Ding
  • 182
  • 2
  • 6
0

Try this

const data = [
    {
        type1: {
            id: "1",
            value: "val1",
        },
        type2: [
            {
                id: "2",
                value: "val2",
            },
        ],
    },
    {
        type1: null,
        type2: [
            {
                id: "5",
                value: "val5",
            },
        ],
    },
    {
        type1: {
            id: "3",
            value: "val3",
        },
        type2: [],
    },
    {
        type1: {
            id: "4",
            value: "val4",
        },
        type2: [
            {
                id: "7",
                value: "val7",
            },
        ],
    },
];
const result = data.find((object) => {
    const { type2 } = object;
    if (!type2) {
        return false;
    }

    const [ firstFromType2 ] = type2;
    if (!firstFromType2) {
        return false;
    }

    return firstFromType2.id === '7';
});
console.log(result);
kellymandem
  • 1,709
  • 3
  • 17
  • 27
0
arr.find(({type2}) => type2[0]?.id === '7')?.type2[0].value
Trevor Dixon
  • 23,216
  • 12
  • 72
  • 109
0

const data = [{"type1":{"id":"1","value":"val1"},"type2":[{"id":"2","value":"val2"}]},{"type1":null,"type2":[{"id":"5","value":"val5"}]},{"type1":{"id":"3","value":"val3"},"type2":[]},{"type1":{"id":"4","value":"val4"},"type2":[{"id":"7","value":"val7"}]}]

console.log(data.flatMap(i=>i.type2).find(({id})=>id==='7')?.value)
Andrew Parks
  • 6,358
  • 2
  • 12
  • 27
-1

Assign the array to a const and make a nested map.

const foundObj = array.map(obj => obj.type2.map(type2obj => type2obj.id === '7')));

const value = foundObj.value

fiddle here: https://jsfiddle.net/

Wesley van S
  • 69
  • 1
  • 12