0

I have a case where I have Array of Arrays and each inner array has multiple objects as below:

const arrayOfArraysOfObjects = [
  [
    { name: 'abv', id: '1212' },
    { name: 'gfgf', id: '887' },
    { name: 'John', id: '886' }
  ],
  [
    { name: 'abv', id: '1242' },
    { name: 'gfgf', id: '837' },
    { name: 'John', id: '816' }
  ],
  [
    { name: 'abv', id: '1242' },
    { name: 'gfgf', id: '837' },
    { name: 'John', id: '896' }
  ]
];

I want to pull out the array which contains the object whose property id is matching with any particular number, say 896

With ES6 I tried this

rawList = arrayOfArraysOfObjects.filter(obj => Object.keys(obj).reduce((acc, curr) => acc || obj[curr] === my.id, false));
return rawList;

here my.id is 896

but this didn't work, MY EXPECTED OUTPUT:

[
   { name: 'abv', id: '1012' },
   { name: 'gfgf', id: '881' },
   { name: 'John', id: '896' }
]

hence, tried correcting it as:

rawList = arrayOfArraysOfObjects.map((apprGrp) => {
  const filteredList = apprGrp.filter(obj => Object.keys(obj).reduce((acc, curr) => acc || obj[curr] === my.id, false));
  return filteredList;
});

this also didnt gave me the above metioned desired result. Please suggest how can I correct this to get the desired results

3
  • Whats your expected output ? Commented Nov 21, 2020 at 7:12
  • Highlighted the expected with text MY EXPECTED OUTPUT: Commented Nov 21, 2020 at 7:19
  • Can you check my answer ? Commented Nov 21, 2020 at 7:19

3 Answers 3

3

You can simply use a array filter and a find to filter by the item

const arrayOfArraysOfObjects = [
  [
   { name: 'abv', id: '1212' },
   { name: 'gfgf', id: '887' },
   { name: 'John', id: '886' }
  ],
  [
   { name: 'abv', id: '1242' },
   { name: 'gfgf', id: '837' },
   { name: 'John', id: '816' }
  ],
  [
   { name: 'abv', id: '1242' },
   { name: 'gfgf', id: '837' },
   { name: 'John', id: '896' }
  ]
];

console.log(arrayOfArraysOfObjects.filter(x => x.find(y => y.id === '896')).flat());

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

2 Comments

this would still give me Array of Array of Object, I am expecting an array of object only
Perfect!! Answer Perfect solution.. Thanks Guru
1

You can check this solution.

const data = [
  [
    { name: "abv", id: "1212" },
    { name: "gfgf", id: "887" },
    { name: "John", id: "886" },
  ],
  [
    { name: "abv", id: "1242" },
    { name: "gfgf", id: "837" },
    { name: "John", id: "816" },
  ],
  [
    { name: "abv", id: "1242" },
    { name: "gfgf", id: "837" },
    { name: "John", id: "896" },
  ],
];

const filterData = (id) => data.filter((arr) => arr.some((obj) => obj.id == id));

console.log(filterData(837));

2 Comments

ideally this would have been my answer if I needed more than one Array which contains this ID, My case would have this ID in only one array
@Noob oh i see, then just using a flat will solve your issue
1

Since you are looking to find a single sub-array with the required id use Array.find() instead of Array.filter(). Use Array.some() to check if the current sub-array contains the id:

const data = [[{"name":"abv","id":"1212"},{"name":"gfgf","id":"887"},{"name":"John","id":"886"}],[{"name":"abv","id":"1242"},{"name":"gfgf","id":"837"},{"name":"John","id":"816"}],[{"name":"abv","id":"1242"},{"name":"gfgf","id":"837"},{"name":"John","id":"896"}]];

const findSubArray = id => data.find(arr => arr.some(o => o.id === id));

console.log(findSubArray('837'));

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.