我有JSON值,如下所示
{
"data": {
"store": "54",
"CountSheet": {
"1": {
"totalInventory": 60,
"totalInventoryCost": 360,
"productCost": 20,
"vendoreCode": "1001",
"vendorName": "Sample 1"
},
"2": {
"totalInventory": 70,
"totalInventoryCost": 360,
"productCost": 30,
"vendoreCode": "1002",
"vendorName": "Sample 2"
}
}
}
}
我想在Countsheet中删除索引值“1”和“2” 如何删除它,请帮我实现这个我想要JSON如下
{
"data": {
"store": "54",
"CountSheet": [
{
"totalInventory": 60,
"totalInventoryCost": 360,
"productCost": 20,
"vendoreCode": "1001",
"vendorName": "Sample 1"
},
{
"totalInventory": 70,
"totalInventoryCost": 360,
"productCost": 30,
"vendoreCode": "1002",
"vendorName": "Sample 2"
}
]
}
}
答案 0 :(得分:1)
var input = {
"data": {
"store": "54",
"CountSheet": {
"1": {
"totalInventory": 60,
"totalInventoryCost": 360,
"productCost": 20,
"vendoreCode": "1001",
"vendorName": "Sample 1"
},
"2": {
"totalInventory": 70,
"totalInventoryCost": 360,
"productCost": 30,
"vendoreCode": "1002",
"vendorName": "Sample 2"
}
}
}
}
var arr = Object.keys(input.data.CountSheet).map(function (key) { return input.data.CountSheet[key]; });
input.data.CountSheet = arr;
console.log(input);

答案 1 :(得分:0)
由于你在CountSheet
索引中只有两个索引1和2,我假设只有两个索引,所以这段代码工作正常。
var jsonData = {
"data": {
"store": "54",
"CountSheet": {
"1": {
"totalInventory": 60,
"totalInventoryCost": 360,
"productCost": 20,
"vendoreCode": "1001",
"vendorName": "Sample 1"
},
"2": {
"totalInventory": 70,
"totalInventoryCost": 360,
"productCost": 30,
"vendoreCode": "1002",
"vendorName": "Sample 2"
}
}
}
};
var obj1 = jsonData.data.CountSheet['1'];
var obj2= jsonData.data.CountSheet['2'];
jsonData.data.CountSheet = [];
jsonData.data.CountSheet.push(obj1);
jsonData.data.CountSheet.push(obj2);
console.log(jsonData);

或者,如果该对象中有两个以上的索引,则使用此代码。它表现动态
var jsonData = {
"data": {
"store": "54",
"CountSheet": {
"1": {
"totalInventory": 60,
"totalInventoryCost": 360,
"productCost": 20,
"vendoreCode": "1001",
"vendorName": "Sample 1"
},
"2": {
"totalInventory": 70,
"totalInventoryCost": 360,
"productCost": 30,
"vendoreCode": "1002",
"vendorName": "Sample 2"
}
}
}
};
var tempArray = [];
var keys = Object.keys(jsonData.data.CountSheet);
for(var i=0; i<keys.length; i++){
var key = keys[i];
var obj = jsonData.data.CountSheet[key];
tempArray.push(obj);
}
jsonData.data.CountSheet = tempArray;
console.log(jsonData);
&#13;