Home > Mobile >  Store item from array of objects in new array
Store item from array of objects in new array

Time:04-08

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.

CodePudding user response:

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 );

CodePudding user response:

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)

CodePudding user response:

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);


CodePudding user response:

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!

CodePudding user response:

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);

  • Related