简体   繁体   中英

flatening the arrays inside of an array

i have an array of objects to each object there is a property called app is there, i need to filter with another array if the check array has the same app then filter and get that one only.

I am able to get it but my problem is i am getting as nested array i need to get a single array of 4 objects.

Please see the below code. Also is there any other way to have the expected output.

 let data = [{ app: "test1", key: 1 }, { app: "test2", key: 2 }, { app: "test1", key: 3 }, { app: "test2", key: 3 }] let checkArr = ["test1", "test2", "test3"] let result = checkArr.map(ch => data.filter(da => da.app === ch)) console.log(result) 

Expected Output

[{
    "app": "test1",
    "key": 1
}, {
    "app": "test1",
    "key": 3
}, {
    "app": "test2",
    "key": 2
}, {
    "app": "test2",
    "key": 3
}]

You can use Array.flatMap() (not supported by IE/Edge):

 let data = [{app: "test1", key: 1}, {app: "test2", key:2}, {app: "test1", key:3}, {app: "test2", key:3}] let checkArr = ["test1", "test2", "test3"] let result = checkArr.flatMap(ch => data.filter(da => da.app === ch)) console.log(result) 

Or flatten by spreading into Array.concat() :

 let data = [{app: "test1", key: 1}, {app: "test2", key:2}, {app: "test1", key:3}, {app: "test2", key:3}] let checkArr = ["test1", "test2", "test3"] let result = [].concat(...checkArr.map(ch => data.filter(da => da.app === ch))) console.log(result) 

Working solution:

let data = [{app: "test1", key: 1}, {app: "test2", key:2}, {app: "test1", key:3}, {app: "test2", key:3}]

let checkArr = ["test1", "test2", "test3"]

let result = data.filter(item => {
    return checkArr.indexOf(item.app)!== -1
})

console.log(result)

fiddle: https://jsfiddle.net/b9vL7ohg/5/

With map you first associate to each name in checkArr the array data.filter(...) of all the apps matching that name.

If you want to flatten this array, the clearest way is to concatenate all the results with a reduce :

checkArr
    .map(name => data.filter(d => d.app === name))
    .reduce((a,b) => [...a, ...b]);

The expected output doesn't appear to be a flattened array. It appears to be the output of

console.log(JSON.stringify(data));

To do your filtering, you would change the predicate:

data.filter((d) => checkArr.indexOf(d.app) !== -1);

To flatten arrays, I would tend to go with

data.reduce((a, innerArray) => {
  a.push(...innerArray);
  return a;
}, []);

This will start with an empty array and incrementally add all of the elements of the subarrays to it. The ... prefix operator on an array converts it to an expanded list of the elements, which are passed into push for insertion without their array wrapper.

Simply use .filter to filter the data object array

 let data = [{app: "test1", key: 1}, {app: "test2", key:2}, {app: "test1", key:3}, {app: "test2", key:3}] let checkArr = ["test1", "test2", "test3"]; var arr = data.filter(o => checkArr.indexOf(o.app) > -1); console.log(arr) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM