如何从另一个对象的键中获取一个对象的值?

时间:2019-09-26 13:35:13

标签: javascript

具有:

const colors = [
  {id: 1, name: "blue"},
  {id: 2, name: "red"},
  {id: 3, name: "green"}
];

并且:

const shirts = [
  {id: 1, color: 2},
  {id: 2, color: 3},
  {id: 3, color: 2},
  {id: 4, color: 1},
  {id: 5, color: 3}
];

如何通过颜色中的shirts引用id中的颜色?我需要以shirt.color = blue结尾。

我正在考虑类似的事情,但无法正确解决:

if (shirt.color === color.id) {
   shirt.color = color.name;
}

谢谢!

6 个答案:

答案 0 :(得分:1)

您可以使用forEach

    const colors = [
      {id: 1, name: "blue"},
      {id: 2, name: "red"},
      {id: 3, name: "green"}
    ];

    const shirts = [
      {id: 1, color: 2},
      {id: 2, color: 3},
      {id: 3, color: 2},
      {id: 4, color: 1},
      {id: 5, color: 3}
    ];

   
shirts.forEach(e=>{
  colors.forEach(c=>{
    if(e.color == c.id ){
      e.color = c.name;
    }
  })
})

    console.log(shirts);

答案 1 :(得分:0)

为此,您可以使用Array.map()来循环colors,然后使用Array.filter()来获得所有与color.id === shirt.color匹配的衬衫。然后,您可以按预期将颜色名称分配给所有衬衫对象。

const colors = [
  {id: 1, name: "blue"},
  {id: 2, name: "red"},
  {id: 3, name: "green"}
];

const shirts = [
  {id: 1, color: 2},
  {id: 2, color: 3},
  {id: 3, color: 2},
  {id: 4, color: 1},
  {id: 5, color: 3}
];

colors.map((color) => {
  let matchShirt = shirts.filter((shirt) => color.id === shirt.color);
  matchShirt.forEach((shirt) => shirt.color = color.name);
});
console.log(shirts);

答案 2 :(得分:0)

您可以使用Map并使用颜色名称创建新对象。

const
    colors = [{ id: 1, name: "blue" }, { id: 2, name: "red" }, { id: 3, name: "green" }],
    shirts = [{ id: 1, color: 2 }, { id: 2, color: 3 }, { id: 3, color: 2 }, { id: 4, color: 1 }, { id: 5, color: 3 }],
    colorMap = new Map(colors.map(({ id, name }) => [id, name])),
    result = shirts.map(o => ({ ...o, name: colorMap.get(o.color) }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

const colors = [
  {id: 1, name: "blue"},
  {id: 2, name: "red"},
  {id: 3, name: "green"}
];

const shirts = [
  {id: 1, color: 2},
  {id: 2, color: 3},
  {id: 3, color: 2},
  {id: 4, color: 1},
  {id: 5, color: 3}
];

shirts.forEach(s => {
	s.color = (colors.find(c => c.id === s.color) || {}).name;
});

console.log(shirts);

答案 4 :(得分:0)

如果将对象用作颜色代码,则它使您的生活变得更加轻松。它避免了将其循环并转换为类似于此的格式以便快速查找的情况。

let spyOnCreateEntitlement = spyOn(component,"func1").and.callThrough();
let spyOnSetRecord = spyOn(component,"func2").and.callThrough();
fixture.detectChanges();
expect(func1).toHaveBeenCalled();//passes
expect(func2).toHaveBeenCalled();//fails!!!!

如果确实必须采用这种格式,那么转换

func1(){
 func2();
}

func2(){
 ...some logic...
}

答案 5 :(得分:0)

给出

const colors = [
  {id: 1, name: "blue"},
  {id: 2, name: "red"},
  {id: 3, name: "green"}
];

上方colors的形状是带有名称和ID的对象的数组。这使代码不必要地笨拙。

通过使colors为对象,我们可以大大简化事情。

const colors = {
  1: "blue",
  2: "red",
  3: "green"
};

const shirts = [
  {id: 1, color: 2},
  {id: 2, color: 3},
  {id: 3, color: 2},
  {id: 4, color: 1},
  {id: 5, color: 3}
];

function assignShirtColor(shirt) {
  shirt.color = colors[shirt.color];
}

那么我们要做的就是写

shirts.forEach(assignShirtColor);