0

Trying to filter array of array objects. when variable matches with array of object value of dropDownOne key 'filterValue', then it will return dropDownTwo array,

 let testName = ‘ filterValue’

 var nestedArray =  [
      [
          {
              dropDownOne: {
                  key: "filterValue",
                  value: "test1"
              },
              dropDownTwo: [
                  {
                      key: "retrieveArrKey1",
                      value: "test123"
                  },
                  {
                      key: "retrieveArrKey2",
                      value: "test345"
                  }
              ]
          }
      ],

[
          {
              dropDownOne: {
                  key: "NoFilter",
                  value: "test2"
              },
              dropDownTwo: [
                  {
                      key: "dropDown2",
                      value: "test"
                  },
                  {
                      key: "dropDown3",
                      value: "test"
                  }
              ]
          }
      ]
    ]




  Output = 
    
     dropDownTwo:[
                      {
                          key: "retrieveArrKey1",
                          value: "test123"
                      },
                      {
                          key: "retrieveArrKey2",
                          value: "test345"
                      }
                  ]

Tried with this

 let filterObj = nestedArray.filter((arr => arr.filter(value => {
        if (value[0].dropDownOne.key === 'filterValue') {
          return arr[1];
        }
      }))

But did not get the correct result

2
  • 2
    filter needs to return boolean value Commented Dec 19, 2022 at 12:57
  • 1
    do you really have the outer objects wrapped insiide of an array with only one object? Commented Dec 19, 2022 at 13:00

4 Answers 4

2

We can use Array.flat() and Array.flatMap() combined with Array.filter() to do it

let result = nestedArray.flat().filter(e => e.dropDownOne.key === testName).flatMap(e => e.dropDownTwo)
console.log(result)

let testName = `filterValue`

 var nestedArray =  [
      [
          {
              dropDownOne: {
                  key: "filterValue",
                  value: "test1"
              },
              dropDownTwo: [
                  {
                      key: "retrieveArrKey1",
                      value: "test123"
                  },
                  {
                      key: "retrieveArrKey2",
                      value: "test345"
                  }
              ]
          }
      ],

[
          {
              dropDownOne: {
                  key: "NoFilter",
                  value: "test2"
              },
              dropDownTwo: [
                  {
                      key: "dropDown2",
                      value: "test"
                  },
                  {
                      key: "dropDown3",
                      value: "test"
                  }
              ]
          }
      ]
    ]
    
let result = nestedArray.flat().filter(e => e.dropDownOne.key === testName).flatMap(e => e.dropDownTwo)
console.log(result)

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

Comments

1

You can do it in this way :

nestedArray.filter(arr => arr.find(item=> item.dropDownOne.key === 'filterValue'))[0][0].dropDownTwo

var nestedArray =  [
      [
          {
              dropDownOne: {
                  key: "filterValue",
                  value: "test1"
              },
              dropDownTwo: [
                  {
                      key: "retrieveArrKey1",
                      value: "test123"
                  },
                  {
                      key: "retrieveArrKey2",
                      value: "test345"
                  }
              ]
          }
      ],

[
          {
              dropDownOne: {
                  key: "NoFilter",
                  value: "test2"
              },
              dropDownTwo: [
                  {
                      key: "dropDown2",
                      value: "test"
                  },
                  {
                      key: "dropDown3",
                      value: "test"
                  }
              ]
          }
      ]
    ]




const result = nestedArray.filter(arr => arr.find(item=> item.dropDownOne.key === 'filterValue'))[0][0].dropDownTwo


console.log(result)

Comments

1

You could use both flat() to flatten the nestedArray, then use the find() method to find the object with a dropDownOne.key that matches the value of testName.

it should then return the dropDownTwo property of that object.

let result = nestedArray.flat().find(obj => obj.dropDownOne.key === testName);
let filterObj = result ? result.dropDownTwo : [];
    
console.log(filterObj);

a full running example:

let testName = 'filterValue';

var nestedArray = [
  [{
    dropDownOne: {
      key: "filterValue",
      value: "test1"
    },
    dropDownTwo: [{
        key: "retrieveArrKey1",
        value: "test123"
      },
      {
        key: "retrieveArrKey2",
        value: "test345"
      }
    ]
  }],

  [{
    dropDownOne: {
      key: "NoFilter",
      value: "test2"
    },
    dropDownTwo: [{
        key: "dropDown2",
        value: "test"
      },
      {
        key: "dropDown3",
        value: "test"
      }
    ]
  }]
]

let result = nestedArray.flat().find(obj => obj.dropDownOne.key === testName);
let filterObj = result ? result.dropDownTwo : [];
    
console.log(filterObj);

2 Comments

Will throw an error in case, find returned undefined
oops moment solved! thanks :)
1

Don't like other solutions due to too many array iterations. You could make a single run using reduce function which is a grandfather of many JS Array functions. It looks a bit worse than flat-filter-flat-..., but will probably work way faster due to much lower array iterations.

const testName = 'filterValue';
const nestedArray = [
  [{
    dropDownOne: {key: 'filterValue', value: 'test1'},
    dropDownTwo: [
      {key: 'retrieveArrKey1', value: 'test123'},
      {key: 'retrieveArrKey2', value: 'test345'},
    ],
  }],
  [{
    dropDownOne: {key: 'NoFilter', value: 'test2'},
    dropDownTwo: [
      {key: 'dropDown2', value: 'test'},
      {key: 'dropDown3', value: 'test'},
    ],
  }],
];

const result = nestedArray.reduce((acc, nestedItem) => {
  nestedItem.forEach(item => {
    if (item.dropDownOne.key === testName) {
      acc.push(item.dropDownTwo);
    }
  });
  return acc;
}, []);

console.log(result);

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.