过滤和克隆对象属性

时间:2016-12-11 04:47:37

标签: javascript arrays node.js object

我有2个对象数组: itemsList itemsFetched 。每个数组中的所有对象都具有相同的结构(键/值的nr)。其中一个键具有相同的“含义”但名称不同( itemList 上的 item_id itemsFetched 上的 id )。他们的价值是一样的。

我需要过滤 itemsList 数组,只留下 item_id 值等于 id 值的对象 itemsFetched 即可。然后将 itemsFetched 数组(与item_id = id匹配)上的每个对象的键/值计数复制(添加)到已过滤的数组。

我有一个正常工作的代码,但我确信它不是解决此问题的最佳方法。我之前已经问了类似的东西(关于“过滤器”部分)解决了我的问题,但由于我必须在过滤后添加“计数”部分,我最终重构了整个事情。

itemsList (示例)

[
  {
      "id": 0,
      "name": "Egg",
      "img": "http://www.serebii.net/pokemongo/items/egg.png"
  },
  {
      "id": 1,
      "name": "Pokeball",
      "img": "http://www.serebii.net/pokemongo/items/20pokeballs.png"
  },
  {
      "id": 2,
      "name": "Greatball",
      "img": "http://www.serebii.net/pokemongo/items/greatball.png"
  },
  { 
      "id": 401,
      "name": "Incense",
      "img": "http://www.serebii.net/pokemongo/items/incense.png" 
  },
  { 
      "id": 901,
      "name": "Incubator (Unlimited)",
      "img": "http://www.serebii.net/pokemongo/items/eggincubator.png"
  }
]

itemsFetched (示例)

[ 
  {
    "item_id": 1, 
    "count": 50,
    "unseen": true
  },
  {
    "item_id": 401,
    "count": 2, 
    "unseen": true
  },
  { 
    "item_id": 901, 
    "count": 1, 
    "unseen": true 
  }
]

resultArray (最终我想要的)

[
  { 
    "id": 1,
    "name": "Pokeball",
    "count": 50,
    "img": "http://www.serebii.net/pokemongo/items/20pokeballs.png",
  },
  { 
    "id": 401,
    "name": "Incense",
    "count": 2,
    "img": "http://www.serebii.net/pokemongo/items/incense.png"
  },
  { 
    "id": 901,
    "name": "Incubator (Unlimited)",
    "count": 1,
    "img": "http://www.serebii.net/pokemongo/items/eggincubator.png"
  }
]

我当前的代码(工作)

let arr = [];
itemsFetched.forEach((item) => {
  itemsList.forEach((item2) => {
    if (item.item_id === item2.id) {
      arr.push({
        "id": item.item_id,
        "name": item2.name,
        "count": item.count,
        "img": item2.img
      });
    }
  });
});

PS:我可以使用ES6 / 7语法/功能。

4 个答案:

答案 0 :(得分:2)

您可以使用哈希映射来复杂地减少时间,您的算法为O(m*n),以下为O(m+n+r)

const itemsMap = itemsList.reduce((map, item) => {
    map[item.id] = item
    return map
}, {})
const results = itemsFetched
    .filter((item) => itemsMap.hasOwnProperty(item.item_id))
    .map((item) => ({
        id: item.item_id,
        name: itemsMap[item.item_id].name,
        count: item.count,
        img: itemsMap[item.item_id].img,
    }))

答案 1 :(得分:1)

for ... of循环(ES6功能)与Array#map结合使用。

这使得第一次找到匹配时return合并对象变得更加容易,这是一个逻辑优化,因为两个列表都不应包含多个具有给定id的条目。

const result = itemsFetched.map(data => {
  for (let item of itemsList) {
    if (data.item_id === item.id) {
      return {
        id: item.id,
        name: item.name,
        count: data.count,
        img: item.img
      }
    }
  }
})

<强>段:

const itemsList = [{
  "id": 0,
  "name": "Egg",
  "img": "http://www.serebii.net/pokemongo/items/egg.png"
}, {
  "id": 1,
  "name": "Pokeball",
  "img": "http://www.serebii.net/pokemongo/items/20pokeballs.png"
}, {
  "id": 2,
  "name": "Greatball",
  "img": "http://www.serebii.net/pokemongo/items/greatball.png"
}, {
  "id": 401,
  "name": "Incense",
  "img": "http://www.serebii.net/pokemongo/items/incense.png"
}, {
  "id": 901,
  "name": "Incubator (Unlimited)",
  "img": "http://www.serebii.net/pokemongo/items/eggincubator.png"
}]

const itemsFetched = [{
  "item_id": 1,
  "count": 50,
  "unseen": true
}, {
  "item_id": 401,
  "count": 2,
  "unseen": true
}, {
  "item_id": 901,
  "count": 1,
  "unseen": true
}]


const result = itemsFetched.map(data => {
  for (let item of itemsList) {
    if (data.item_id === item.id) {
      return {
        id: item.id,
        name: item.name,
        count: data.count,
        img: item.img
      }
    }
  }
})

console.log(result)

答案 2 :(得分:0)

改进的一种方法是使用for..of语句而不是forEach作为内部循环。这有助于在id匹配时从循环中断开。没有直接的方法来打破forEach方法。

let arr = [];
itemsFetched.forEach((item) => {
  for (let item2 of itemsList) {
    if (itemsFetched.item_id === itemsList.id) {
      arr.push({
        "id": itemsFetched.item_id,
        "name": itemsList.name,
        "count": itemsFetched.count,
        "img": itemsList.img
      });
      break;
    }
  }
});

答案 3 :(得分:-1)

喜欢这个吗?

&#13;
&#13;
var itemsList = [
  {
      "id": 0,
      "name": "Egg",
      "img": "http://www.serebii.net/pokemongo/items/egg.png"
  },
  {
      "id": 1,
      "name": "Pokeball",
      "img": "http://www.serebii.net/pokemongo/items/20pokeballs.png"
  },
  {
      "id": 2,
      "name": "Greatball",
      "img": "http://www.serebii.net/pokemongo/items/greatball.png"
  },
  { 
      "id": 401,
      "name": "Incense",
      "img": "http://www.serebii.net/pokemongo/items/incense.png" 
  },
  { 
      "id": 901,
      "name": "Incubator (Unlimited)",
      "img": "http://www.serebii.net/pokemongo/items/eggincubator.png"
  }
];

var itemsFetched = [ 
  {
    "item_id": 1, 
    "count": 50,
    "unseen": true
  },
  {
    "item_id": 401,
    "count": 2, 
    "unseen": true
  },
  { 
    "item_id": 901, 
    "count": 1, 
    "unseen": true 
  }
]



let arr = [];
itemsFetched.forEach((item) => {
  itemsList.forEach((item2) => {
    if (item.item_id == item2.id) {
      arr.push({
        "id": item.item_id,
        "name": item2.name,
        "count": item.count,
        "img": item2.img
      });
    }
  });
});

console.log(arr);
&#13;
&#13;
&#13;