Javascript 如何从字典中提取项目

Javascript 如何从字典中提取项目,javascript,reactjs,Javascript,Reactjs,我有一本字典,看起来像这样: "Player Details": { "Sally Smith": { "player_id": 2, "color_one": "blue", "color_two": "red" }, "John Smith": { "player_id": 4, "color_one": "white", "color_two": "black"

我有一本字典,看起来像这样:

"Player Details": {
    "Sally Smith": {
        "player_id": 2,
        "color_one": "blue",
        "color_two": "red"
    },
    "John Smith": {
        "player_id": 4,
        "color_one": "white",
        "color_two": "black"
    }
}
"Player details": [
    {
        "player_id": "2",
        "color_one": "blue",
        "color_two": "red"
    },
    {
        "player_id": "4",
        "color_one": "white",
        "color_two": "black"
    }
]
我需要它进入一个数组,然后像这样结束:

"Player Details": {
    "Sally Smith": {
        "player_id": 2,
        "color_one": "blue",
        "color_two": "red"
    },
    "John Smith": {
        "player_id": 4,
        "color_one": "white",
        "color_two": "black"
    }
}
"Player details": [
    {
        "player_id": "2",
        "color_one": "blue",
        "color_two": "red"
    },
    {
        "player_id": "4",
        "color_one": "white",
        "color_two": "black"
    }
]
我尝试了以下方法,但“价值”仍然存在:

Object.entries(myDictionary).forEach(([key, value]) => {
   newArray.push({value})
});

我觉得我就快到了,有人能帮忙吗?

只需使用返回数组中对象属性值的方法从对象获取值即可

let数据={
“玩家详细信息”:{
“萨利·史密斯”:{
“玩家id”:2,
“颜色一”:“蓝色”,
“颜色二”:“红色”
},
“约翰·史密斯”:{
“玩家id”:4,
“颜色”:“白色”,
“颜色二”:“黑色”
}
}
}
设res={
“玩家详细信息”:对象.值(数据[“玩家详细信息])
};
//或者如果你不需要阵列的话
设res1=Object.values(数据['Player Details']);
console.log(res)

log(res1)
只需使用返回数组中对象属性值的方法从对象获取值

let数据={
“玩家详细信息”:{
“萨利·史密斯”:{
“玩家id”:2,
“颜色一”:“蓝色”,
“颜色二”:“红色”
},
“约翰·史密斯”:{
“玩家id”:4,
“颜色”:“白色”,
“颜色二”:“黑色”
}
}
}
设res={
“玩家详细信息”:对象.值(数据[“玩家详细信息])
};
//或者如果你不需要阵列的话
设res1=Object.values(数据['Player Details']);
console.log(res)

console.log(res1)
您需要循环查看
myDictionary[“Player Details”]
并将
value
推入
newArray
而不是
{value}

或者您可以使用
Object.values

Object.values(myDictionary["Player Details"])
const myDictionary={
“玩家详细信息”:{
“萨利·史密斯”:{
“玩家id”:2,
“颜色一”:“蓝色”,
“颜色二”:“红色”
},
“约翰·史密斯”:{
“玩家id”:4,
“颜色”:“白色”,
“颜色二”:“黑色”
}
}
}
常量newArray=[];
Object.entries(myDictionary[“播放器详细信息]).forEach(([key,value])=>{
newArray.push(值)
});
log(newArray);
//或者是一条单行线

console.log(Object.values(myDictionary[“Player Details”])
您需要循环查看
myDictionary[“Player Details”]
并将
value
推入
newArray
而不是
{value}

或者您可以使用
Object.values

Object.values(myDictionary["Player Details"])
const myDictionary={
“玩家详细信息”:{
“萨利·史密斯”:{
“玩家id”:2,
“颜色一”:“蓝色”,
“颜色二”:“红色”
},
“约翰·史密斯”:{
“玩家id”:4,
“颜色”:“白色”,
“颜色二”:“黑色”
}
}
}
常量newArray=[];
Object.entries(myDictionary[“播放器详细信息]).forEach(([key,value])=>{
newArray.push(值)
});
log(newArray);
//或者是一条单行线
console.log(Object.values(myDictionary[“Player Details”])您只需使用
让obj={“玩家详细信息”:{
“萨利·史密斯”:{
“玩家id”:2,
“颜色一”:“蓝色”,
“颜色二”:“红色”
},
“约翰·史密斯”:{
“玩家id”:4,
“颜色”:“白色”,
“颜色二”:“黑色”
}
}}
let op={'Player Details':Object.values(obj['Player Details'])}
console.log(op)
您只需使用
让obj={“玩家详细信息”:{
“萨利·史密斯”:{
“玩家id”:2,
“颜色一”:“蓝色”,
“颜色二”:“红色”
},
“约翰·史密斯”:{
“玩家id”:4,
“颜色”:“白色”,
“颜色二”:“黑色”
}
}}
let op={'Player Details':Object.values(obj['Player Details'])}

console.log(op)
{'Player Details':Object.values(obj['Player Details'])}
@PranavCBalan谢谢,但你能详细说明,展示一个例子吗?
{'Player Details':Object.values(obj['Player Details'])}
@PranavCBalan谢谢,但你能详细说明,展示一个例子吗?这很有效,但会产生一个对象,而我需要一个数组。@user3328696:
let res1=Object.values(data['Player Details'])
这可以工作,但会产生一个对象,而我需要一个数组。@user3328696:
让res1=object.values(data['Player Details'])成功了!非常感谢。同样的想法,但我会使用更简洁的东西:constnewarray=[…Object.values(myDictionary[“Player Details”]);成功了!非常感谢。同样的想法,但我会使用更简洁的东西:constnewarray=[…Object.values(myDictionary[“Player Details”]);