我是Javascript和Nodejs的新手,我不知道如何遍历下面的对象数组并删除其数字键。这样,当我提取数组中每个对象的关键帧时,我得到-CCD_ 1。
嵌套对象的数组以这种格式给出
[
{
'0': {
City: 'Kottayam',
'Operator Name': 'GEORGE KUTTY K',
'Operator Id': 1808,
Amount: 3200,
'Transaction Type': 'Received'
},
'1': {
City: 'Kottayam',
'Operator Name': 'SAJI VARGHESE',
'Operator Id': 1723,
Amount: 276.512,
'Transaction Type': 'Received'
},
'2': {
City: 'Kottayam',
'Operator Name': 'SUNEESH K D',
'Operator Id': 1010,
Amount: 600,
'Transaction Type': 'Received'
},
'3': {
City: 'Kottayam',
'Operator Name': 'PRASAD TK',
'Operator Id': 1745,
Amount: 19.8,
'Transaction Type': 'Paid'
}
}
]
我需要每个对象的密钥-['City', 'Operator Name', 'Operator Id', 'Transaction Type']
我想你的意思是——一个对象数组。仔细查看您的数据类型,它是一个无效的数据类型。如果您有大括号{},那么您声明的对象需要一个带值的键。
[
{
City: 'Kottayam',
'Operator Name': 'GEORGE KUTTY K',
'Operator Id': 1808,
Amount: 3200,
'Transaction Type': 'Received'
},
{
City: 'Kottayam',
'Operator Name': 'SAJI VARGHESE',
'Operator Id': 1723,
Amount: 276.512,
'Transaction Type': 'Received'
},
{
City: 'Kottayam',
'Operator Name': 'SUNEESH K D',
'Operator Id': 1010,
Amount: 600,
'Transaction Type': 'Received'
},
{
City: 'Kottayam',
'Operator Name': 'PRASAD TK',
'Operator Id': 1745,
Amount: 19.8,
'Transaction Type': 'Paid'
}
]
对于溶液
let array =
[
{
'0': {
City: 'Kottayam',
'Operator Name': 'GEORGE KUTTY K',
'Operator Id': 1808,
Amount: 3200,
'Transaction Type': 'Received'
},
'1': {
City: 'Kottayam',
'Operator Name': 'SAJI VARGHESE',
'Operator Id': 1723,
Amount: 276.512,
'Transaction Type': 'Received'
},
'2': {
City: 'Kottayam',
'Operator Name': 'SUNEESH K D',
'Operator Id': 1010,
Amount: 600,
'Transaction Type': 'Received'
},
'3': {
City: 'Kottayam',
'Operator Name': 'PRASAD TK',
'Operator Id': 1745,
Amount: 19.8,
'Transaction Type': 'Paid'
}
}
]
console.log(array); //this is your starting array, notice it just has one object in there at index 0
let arrayTransform = []; //this is where we will store our result
for (const [key, value] of Object.entries(array[0])){
console.log(key,value);
arrayTransform.push(value);
}
console.log('array transform looks like', arrayTransform);
//arrayTransform should look like this
//[
// {
// City: 'Kottayam',
// 'Operator Name': 'GEORGE KUTTY K',
// 'Operator Id': 1808,
// Amount: 3200,
// 'Transaction Type': 'Received'
// },
// {
// City: 'Kottayam',
// 'Operator Name': 'SAJI VARGHESE',
// 'Operator Id': 1723,
// Amount: 276.512,
// 'Transaction Type': 'Received'
// },
// {
// City: 'Kottayam',
// 'Operator Name': 'SUNEESH K D',
// 'Operator Id': 1010,
// Amount: 600,
// 'Transaction Type': 'Received'
// },
// {
// City: 'Kottayam',
// 'Operator Name': 'PRASAD TK',
// 'Operator Id': 1745,
// Amount: 19.8,
// 'Transaction Type': 'Paid'
// }
// ]
我想这就是你想要做的
let oldArr = [
{
'0': {
City: 'Kottayam',
'Operator Name': 'GEORGE KUTTY K',
'Operator Id': 1808,
Amount: 3200,
'Transaction Type': 'Received'
}
},
{'1': {
City: 'Kottayam',
'Operator Name': 'SAJI VARGHESE',
'Operator Id': 1723,
Amount: 276.512,
'Transaction Type': 'Received'
}
},
{'2': {
City: 'Kottayam',
'Operator Name': 'SUNEESH K D',
'Operator Id': 1010,
Amount: 600,
'Transaction Type': 'Received'
}
},
{'3': {
City: 'Kottayam',
'Operator Name': 'PRASAD TK',
'Operator Id': 1745,
Amount: 19.8,
'Transaction Type': 'Paid'
}
}
]
let newArr = [];
oldArr.forEach(obj => newArr.push(...Object.values(obj)))
console.log(newArr)
下面提到的格式是错误的,即使您进行了更正,也没有那么大意义,但我会尽力提供帮助。我所能理解的是,您正在尝试提取给定对象的值。在这里的数组中,您可以使用Object.values()
运算符Read here。所以我们可以在这里做两件事,如果你只想要钥匙,你可以做Object.keys()
,它会给你这个
Array(4) [ "0", "1", "2", "3" ]
如果你想提取值并将其保持为数组格式,你可以执行Object.values()
,这将在这里给你的输出
[{ City: "Kottayam", "Operator Name": "GEORGE KUTTY K", "Operator Id": 1808, … },
{ City: "Kottayam", "Operator Name": "SAJI VARGHESE", "Operator Id": 1723, … },
{ City: "Kottayam", "Operator Name": "SUNEESH K D", "Operator Id": 1010, … },
{ City: "Kottayam", "Operator Name": "PRASAD TK", "Operator Id": 1745, … }]
如果你想把这些值包装在另一个Object而不是Array中,只需执行const obj={...array}
我希望这有帮助,如果它有助于,请将答案标记为正确