peter flanagan
peter flanagan

Reputation: 9790

JS get all the arrays within an array

I have an object that looks like the following:

const test = {
  leagues: [
    {
      timezone: "GMT",
      date: "1/2/2",
      premierLeague: [
        { name: "Liverpool", age: 1892 },
        { name: "Manchester Utd", age: 1878 }
      ],
      laLiga: [
        {
          team: "Real Madrid",
          stadium: "Bernabeu"
        },
        {
          team: "Barcelona",
          stadium: "Camp Nou"
        }
      ]
    }
  ]
};

and I want the result to look like

const result = [
  { name: "Liverpool", age: 1892 },
  { name: "Manchester Utd", age: 1878 },
  {
    team: "Real Madrid",
    stadium: "Bernabeu"
  },
  {
    team: "Barcelona",
    stadium: "Camp Nou"
  }
];

I have tried to use flat() but am having trouble getting the arrays within the leagues. The result will be dynamic so I need to be able to get all the arrays within leagues. Can anyone point me in the correct direction to do this?

Upvotes: 0

Views: 89

Answers (4)

Ruan Mendes
Ruan Mendes

Reputation: 92274

This sounds weird, you'll end up with objects of different shapes in the same array. I'm not sure how you'll deal with that.

It looks like you're trying to concatenate every value of test.leagues that is itself an array.

const test = {
  leagues: [{
    timezone: "GMT",
    date: "1/2/2",
    premierLeague: [{
        name: "Liverpool",
        age: 1892
      },
      {
        name: "Manchester Utd",
        age: 1878
      }
    ],
    laLiga: [{
        team: "Real Madrid",
        stadium: "Bernabeu"
      },
      {
        team: "Barcelona",
        stadium: "Camp Nou"
      }
    ]
  }]
};


const output = [];
for (const league of test.leagues) {
  for (const key in league) {
    if (Array.isArray(league[key])) {
      // Push each element in `league[key]` onto `output`
      // so we don't have to flatten it later
      output.push(...league[key]);
    }
  }
}
console.log({
  output
});

Upvotes: 1

Bergi
Bergi

Reputation: 664444

You might be looking for

const result = test.leagues.flatMap(league =>
  Object.values(league).filter(Array.isArray).flat()
);

Upvotes: 2

Jared Smith
Jared Smith

Reputation: 21926

If your object structure doesn't go an deeper than that, this long one-liner should work:

const result = test.leagues.reduce((arr, obj) => Object.values(val).reduce((innerArr, val) => Array.isArray(val) ? innerArr.concat(val) : innerArr, arr), []);

Somewhat ungolfed:

const result = test.leagues.reduce((arr, obj) => {
  return Object.values(val).reduce((innerArr, val) => {
    return Array.isArray(val)
      ? innerArr.concat(val) 
      : innerArr
  }, arr);
}), []);

Upvotes: 3

Jose A
Jose A

Reputation: 11077

Well, I'm also adding my 2 cents over here. I do agree with everyone else. See if this works:

const test = {
  leagues: [
    {
      timezone: "GMT",
      date: "1/2/2",
      premierLeague: [
        { name: "Liverpool", age: 1892 },
        { name: "Manchester Utd", age: 1878 }
      ],
      laLiga: [
        {
          team: "Real Madrid",
          stadium: "Bernabeu"
        },
        {
          team: "Barcelona",
          stadium: "Camp Nou"
        }
      ]
    }
  ]
};

let finalArray = [];

function recursiveArr(obj, arrayToPush) {
    for(const [key, val] of Object.entries(obj)) {
    if(Array.isArray(obj[key])) {
      arrayToPush.push(obj[key]);
      continue;
    }
    
    const type = typeof obj[key];
    
    if(type === "object") {
       recursiveArr(obj[key], arrayToPush);
    }
  }
}

recursiveArr(test.leagues, finalArray);

console.log(finalArray.flat())

Upvotes: 0

Related Questions