最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - how to iterate over nested array object in javascript - Stack Overflow

matteradmin9PV0评论

I would like to know how to iterate through a nested array of objects in Javascipt? I have a sample object named obj. I want to retrieve the object based on condition 'in' is 'string' and 'out' 'number'.

// tried got stuck
const output = [];
    myList.forEach(entry => {
      Object.keys(entry).forEach(key => {
        const entity = entry[key][0];
        if (entity.in === "string" && entity.out === "number") {
          output.push(entity);
        }
      });
    });
var obj = [{
    "ston": [{
      "id": "identity1",
      "in": "string",
      "out": "number",
      "value": 10
    },{
        "id": "identity2",
        "in": "string",
        "out": "number",
        "value": 10
    }],
    "nton": [{
      "id": "identity1",
      "in": "number",
      "out": "number",
      "value": 20
    },{
      "id": "identity2",
      "in": "number",
      "out": "number",
      "value": 30
    }]
  }]

Expected Output

   [{
      "id": "identity1",
      "in": "string",
      "out": "number",
      "value": 10
    },{
        "id": "identity2",
        "in": "string",
        "out": "number",
        "value": 10
    }]

I would like to know how to iterate through a nested array of objects in Javascipt? I have a sample object named obj. I want to retrieve the object based on condition 'in' is 'string' and 'out' 'number'.

// tried got stuck
const output = [];
    myList.forEach(entry => {
      Object.keys(entry).forEach(key => {
        const entity = entry[key][0];
        if (entity.in === "string" && entity.out === "number") {
          output.push(entity);
        }
      });
    });
var obj = [{
    "ston": [{
      "id": "identity1",
      "in": "string",
      "out": "number",
      "value": 10
    },{
        "id": "identity2",
        "in": "string",
        "out": "number",
        "value": 10
    }],
    "nton": [{
      "id": "identity1",
      "in": "number",
      "out": "number",
      "value": 20
    },{
      "id": "identity2",
      "in": "number",
      "out": "number",
      "value": 30
    }]
  }]

Expected Output

   [{
      "id": "identity1",
      "in": "string",
      "out": "number",
      "value": 10
    },{
        "id": "identity2",
        "in": "string",
        "out": "number",
        "value": 10
    }]

Share Improve this question edited Apr 4, 2019 at 3:52 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Apr 4, 2019 at 3:39 miamia 2651 gold badge5 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You can use rebuild that object to a nested array then flatten and finally filter.

var obj = [
  {
    "ston": [
      {"id": "identity1", "in": "string", "out": "number", "value": 10},
      {"id": "identity2", "in": "string", "out": "number", "value": 10}
    ],
    "nton": [
      {"id": "identity1", "in": "number", "out": "number", "value": 20},
      {"id": "identity2", "in": "number", "out": "number", "value": 30}
    ]
  }
];

const tmp = obj.map(e => Object.entries(e).map(([k, v]) => v)).flat(3)

const rs = tmp.filter(e => e.out==='number' && e.in ==='string')

console.log(rs)

Simple recursive function:

var obj = [{
  "ston": [{
    "id": "identity1",
    "in": "string",
    "out": "number",
    "value": 10
  }, {
    "id": "identity2",
    "in": "string",
    "out": "number",
    "value": 10
  }],
  "nton": [{
    "id": "identity1",
    "in": "number",
    "out": "number",
    "value": 20
  }, {
    "id": "identity2",
    "in": "number",
    "out": "number",
    "value": 30
  }]
}];

function getObjects(inVal, outVal) {
  var matches = [];
  obj.forEach(child => {
    Object.values(child).forEach(arr => {
      if (arr.some(e => e.in == inVal && e.out == outVal)) {
        matches.push([...arr.filter(e => e => e.in == inVal && e.out == outVal)]);
      }
    });
  });
  return matches.flat();
}

console.log(getObjects("string", "number"));

Here you have one solution that uses mainly Array.reduce() to iterate over the outter objects of the array, get a flattened array of the values from every outter object to create an array with inner objects and then filter those meeting the condition while saving they in a new array:

var obj = [
  {
    "ston": [
      {"id": "identity1", "in": "string", "out": "number", "value": 10},
      {"id": "identity2", "in": "string", "out": "number", "value": 10}
    ],
    "nton": [
      {"id": "identity1", "in": "number", "out": "number", "value": 20},
      {"id": "identity2", "in": "number", "out": "number", "value": 30}
    ]
  }
];

let res = obj.reduce((acc, o) =>
{
    acc = acc.concat(Object.values(o).flat().filter(
        o => o.in === "string" && o.out === "number"
    ));
    return acc;
}, []);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Other documentation of used methods:

  • Array.concat()
  • Array.flat()
  • Array.filter()
  • Object.values()

Or, after some time thinking, you can use the next simplified version with Array.map()

var obj = [
  {
    "ston": [
      {"id": "identity1", "in": "string", "out": "number", "value": 10},
      {"id": "identity2", "in": "string", "out": "number", "value": 10}
    ],
    "nton": [
      {"id": "identity1", "in": "number", "out": "number", "value": 20},
      {"id": "identity2", "in": "number", "out": "number", "value": 30}
    ]
  }
];

let res = obj.map(Object.values).flat(2).filter(
    o => o.in === "string" && o.out === "number"
);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Post a comment

comment list (0)

  1. No comments so far