reactjs - 获取嵌套数组对象的子集

reactjs - get a subset of an nested array objects

我有一个这样的变量,我将其作为输入传递给 React 应用程序。

const options = [{"label": "Gryffindor", "value": "Gryffindor", "description": "Daring, strong nerve and chivalry.", "color": "#00ffa2"},
      {"label": "Slytherin", "value": "Slytherin", "description": "Cunning and ambitious. Possibly dark wizard.", "color": "#84d2ff"}]

从上面的数组我想实现以下输出:

const TAG_COLORS:any = {
  Gryffindor: '#00ffa2',
  Slytherin: '#84d2ff',
}

有人可以建议如何实现这一目标吗?

认为这就是您所追求的:

编辑。我错了问题。

  const options = [{"label": "Gryffindor", "value": "Gryffindor", "description": "Daring, strong nerve and chivalry.", "color": "#00ffa2"}, {"label": "Slytherin", "value": "Slytherin", "description": "Cunning and ambitious. Possibly dark wizard.", "color": "#84d2ff"}];
 
var TAG_COLOURS = {};
for (var i = 0; i < options.length; i++) {
  TAG_COLOURS[options[i].label] = options[i].color;
}

console.log(TAG_COLOURS);

您可以使用 map 函数创建一个 JavaScript 对象,然后在其上使用 JavaScript 对象方法。

您可以这样做:

var options = [
{
 "label":
     "Gryffindor", "value": "Gryffindor", "description": "Daring, strong nerve and chivalry.", "color": "#00ffa2"}, 
{
 "label": 
     "Slytherin", "value": "Slytherin", "description": "Cunning and ambitious. Possibly dark wizard.", "color": "#84d2ff"}
]


options.map((data)=>{console.log(Object.values(data))})

希望这能回答您的问题