compare object value and return new array

问题: 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 locations 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)
  • 发表于 2019-03-17 21:44
  • 阅读 ( 170 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除