0

I have an array of objects that looks like this:

[{
        "id": 1,
        "name": "Name 1",
        "created": "2022-04-07T18:40:11Z",
        "updated": "2022-04-07T18:40:19Z",
        "deleted": null,
        "accounts": [
            "89084",
            "34342"
        ]
    },
    {
        "id": 2,
        "name": "Name 2",
        "created": "2022-01-07T18:40:11Z",
        "updated": "2022-01-07T18:40:19Z",
        "deleted": null,
        "accounts": [
            "99084",
            "38342"
        ]
    }
]

How do I store all accounts fields into a single array. Notice accounts is an array inside an object inside an array of objects.

1
  • you are probably looking for flatMap Commented Apr 7, 2022 at 20:00

5 Answers 5

3

You can use Array#flatMap. To remove duplicates, if any, you can use new Set().

const output = input.flatMap(({accounts}) => accounts);

const input = [{"id": 1,"name": "Name 1","created": "2022-04-07T18:40:11Z","updated": "2022-04-07T18:40:19Z","deleted": null,"accounts": ["89084","34342"]},{"id": 2,"name": "Name 2","created": "2022-01-07T18:40:11Z","updated": "2022-01-07T18:40:19Z","deleted": null,"accounts": ["99084", "38342"]}];

const output = [...new Set(input.flatMap(({accounts}) => accounts))];

console.log( output );

Sign up to request clarification or add additional context in comments.

2 Comments

I like this approach. I was going to go with map and flat, but this is a touch nicer with the removal of duplicates!
you dropped this } :)
2

Basically you need to use a map function to extract the data into a new array ( by default the map function creates a new array ). So if you had to do that onto your array it'd be something like this:

const newArray =yourArray.map(item => item.accounts)

In case you want all your items into one single array and then you'd actually have no nested arrays you'd use this

const newArray =yourArray.flatMap(item => item.accounts)

Comments

0
let new_array = [];
var data = [
  {
    id: 1,
    name: "Name 1",
    created: "2022-04-07T18:40:11Z",
    updated: "2022-04-07T18:40:19Z",
    deleted: null,
    accounts: ["89084", "34342"],
  },
  {
    id: 2,
    name: "Name 2",
    created: "2022-01-07T18:40:11Z",
    updated: "2022-01-07T18:40:19Z",
    deleted: null,
    accounts: ["99084", "38342"],
  },
];

for (i in data) {
  for (j in data[i].accounts) {
    new_array.push(data[i].accounts[j]);
  }
}

console.log(new_array);


Comments

0

I would approach this the following way:

let consolidated = []
for(entry in data) {
    consolidated = [...consolidated, ...json[entry].accounts]
}

Simple and only uses a single loop. Hope this helps!

1 Comment

I defer to the superior one-liner of Ghost's answer
0

res=[{
        "id": 1,
        "name": "Name 1",
        "created": "2022-04-07T18:40:11Z",
        "updated": "2022-04-07T18:40:19Z",
        "deleted": null,
        "accounts": [
            "89084",
            "34342"
        ]
    },
    {
        "id": 2,
        "name": "Name 2",
        "created": "2022-01-07T18:40:11Z",
        "updated": "2022-01-07T18:40:19Z",
        "deleted": null,
        "accounts": [
            "99084",
            "38342"
        ]
    }
];

var accounts=[];
res.forEach(function(el) { 
    accounts=accounts.concat(el.accounts);
})

console.log(accounts);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.