So here is my Array :

[
    {
      "a": "2",
      "ss": [
        {
          "name": "pourush",
          "age": "25"
        },
        {
          "name": "Mak",
          "age": "25"
        }
      ]
    },
     {
      "b": "3",
      "ss": [
        {
          "name": "James",
          "age": "19"
        },
        {
          "name": "shaun",
          "age": "20"
        }
      ]
    }
  ]

Now , This is an array of objects. I want to loop this array and want to go at "ss" and extract both of the objects.

The result I need is Array of objects which will contain this ss objects.

  {
          "name": "pourush",
          "age": "25"
        },
        {
          "name": "Mak",
          "age": "25"
        },
    {
          "name": "James",
          "age": "19"
        },
        {
          "name": "shaun",
          "age": "20"
        }

]

Write your answer…

6 answers

const list = [
  {
    "a": "2",
    "ss": [
      {
        "name": "pourush",
        "age": "25"
      },
      {
        "name": "Mak",
        "age": "25"
      }
    ]
  },
  {
    "b": "3",
    "ss": [
      {
        "name": "James",
        "age": "19"
      },
      {
        "name": "shaun",
        "age": "20"
      }
    ]
  }
]

function scan(list, key) {
  return list.reduce((a, b) => {
    let obj = b[key] || []
    return a.concat(obj)
  }, [])
}

console.log(scan(list, 'ss'))

This is more dynamic. It also works with "a" or "b".

This is so far the best solution.

Reply to this…

Hashnode is building a friendly and inclusive dev community. Come jump on the bandwagon!

  • 💬 A beginner friendly place

  • 🧠 Stay in the loop and grow your knowledge

  • 🍕 >500K developers share programming wisdom here

  • ❤️ Support the growing dev community!

Register ( 500k+ developers strong 👊)

This should do it

return yourArray.reduce((accumulator, current) => {
  current.ss.forEach(ss => {
    accumulator.push(ss)
  })

  return accumulator
}, [])

Just to throw another method onto the fire -- modern ECMAScript only:

var result = [];
for (var i of yourData) result = result.concat(i.ss);

I favor the for loop approaches -- the reduce/map/foreach methodologies may seem 'simpler' but in reality the overhead of the function callbacks and the functions themselves can end up two to five times slower than just brute force looping. More so in modern ECMAScript where for/of is now an (ridiculously overdue) addition to the language.

oh, and depending on the browser engine this may be faster -- and does use less memory across all of them.

var result = [];
for (var i of originalArr) result.push.apply(result, i.ss));

It's strange, but it works. The assignment of "result = result.concat" means memory allocation for the result of the concat, and the overhead of releasing the old value of the result variable. A push just allocates a new pointer/space for the new element on the existing array instead of a full assignment/release of the entire array. Faster in some browsers, slower in others, less memory thrashing. (though potential for higher fragmentation)

If I was deploying for just v8 -- such as an electron or nw.js app -- I'd go with the latter. If making a website where Edge/Chakra is still the slowpoke in the room I'd go with the former.

Here you go :)

let originalArr = [
    {
      "a": "2",
      "ss": [
        {
          "name": "pourush",
          "age": "25"
        },
        {
          "name": "Mak",
          "age": "25"
        }
      ]
    },
     {
      "b": "3",
      "ss": [
        {
          "name": "James",
          "age": "19"
        },
        {
          "name": "shaun",
          "age": "20"
        }
      ]
    }
  ];

let newArr = [];
for (let i in originalArr) {
    for (let j in originalArr[i].ss) {
        newArr.push(originalArr[i].ss[j]);
    }
}
//test it out:
console.log(newArr);

Hey, I think this one much simpler.

let newArr = [];
    originalArr.map(item => {
      for (let i in item.ss) {
        newArr.push(item.ss);
      }
    });

//test it out:
 console.log(newArr);
Load more responses