问题:
I have this two arrays :
const data = [
{ type: 'type1', location: 23 },
{ type: 'type2', location: 37 },
{ type: 'type3', location: 61 },
{ type: 'type4', locat...
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
I have this two arrays :
const data = [
{ type: 'type1', location: 23 },
{ type: 'type2', location: 37 },
{ type: 'type3', location: 61 },
{ type: 'type4', location: 67 }
]
const com = [
{ name: "com1", location: 36 },
{ name: "com2", location: 60 }
]
And i want to test if the locationComment
in the array com
+1 is equal to the locationMethod
in the array data
, if yes then i want to have something like this :
const array = [
{type: 'type2', name: "com1"},
{type: 'type3', name: "com2"}
]
Here is my code :
const result = data.map((x)=>{
const arr = [];
com.map((y)=>{
if(x.location == y.location+1){
arr.push({
type: x.type,
name: y.name,
location: x.location
})
}
})
return arr;
});
This is the output i get:
[ [],
[ { type: 'type2', name: 'com1', location: 37 } ],
[ { type: 'type3', name: 'com2', location: 61 } ],
[] ]
回答1:
Because you aren't sure whether there will be a match for every element in the com
array, you should use reduce
:
const data = [
{ type: 'type1', location: 23 },
{ type: 'type2', location: 37 },
{ type: 'type3', location: 61 },
{ type: 'type4', location: 67 }
];
const com = [
{ name: "com1", location: 36 },
{ name: "com2", location: 60 }
];
const output = com.reduce((a, { name, location }) => {
const found = data.find(item => item.location === location + 1);
if (found) {
a.push({ type: found.type, name });
}
return a;
}, []);
console.log(output);
If the location
s are unique, you could reduce time complexity to O(N)
by transforming data
into an object indexed by location
first:
const data = [
{ type: 'type1', location: 23 },
{ type: 'type2', location: 37 },
{ type: 'type3', location: 61 },
{ type: 'type4', location: 67 }
];
const com = [
{ name: "com1", location: 36 },
{ name: "com2", location: 60 }
];
const dataByLoc = data.reduce((a, item) => {
a[item.location] = item;
return a;
}, {});
const output = com.reduce((a, { name, location }) => {
const found = dataByLoc[location + 1];
if (found) {
a.push({ type: found.type, name });
}
return a;
}, []);
console.log(output);
回答2:
If the one line solution doesn't appeal to you, the following code should work too using a combination of .map() and .find() .
const data = [
{ type: 'type1', location: 23 },
{ type: 'type2', location: 37 },
{ type: 'type3', location: 61 },
{ type: 'type4', location: 67 }
]
const com = [
{ name: "com1", location: 36 },
{ name: "com2", location: 60 }
]
// Final Array Output with merged properties
let finalArr = []
data.forEach(locationMethodObj => {
// Fetching the type
let finalObj = {
type: locationMethodObj.type
}
// Checking if a location match exists
const foundObj = com.find(locationCommentObj => (locationCommentObj.location + 1) ===
locationMethodObj.location
)
// If a match is found, then append name and push
if(foundObj){
finalObj.name = foundObj.name
finalArr.push(finalObj)
}
})
console.log(finalArr)