我想从我的[object object]
删除所有json
而不是基于密钥,但是基于值i,e([object object]
)。
这是我的json
var allProductSpecification = {
"Vinyl_Backlite": [{
"Price": "280",
"Quantity": "1",
"Amount": "280"
}],
"Steel": [{
"Price": "18",
"Quantity": "1",
"Amount": "18"
}],
"0": "[object Object]",
"1": "[object Object]"
}
allProductSpecification = JSON.parse(allProductSpecification);
delete allProductSpecification[0];
delete allProductSpecification[1];
console.log(allProductSpecification);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
这是我如何从键中删除[object object]:0
delete allProductSpecification[0];
问题:如何动态[object object]
动态
答案 0 :(得分:2)
您可以使用条件array#reduce
仅添加值不为[object Object]
的键。
var allProductSpecification = { "Vinyl_Backlite": [{ "Price": "280", "Quantity": "1", "Amount": "280" }], "Steel": [{ "Price": "18", "Quantity": "1", "Amount": "18" }], "0": "[object Object]", "1": "[object Object]" },
result = Object.keys(allProductSpecification).reduce((r,k) => {
if(allProductSpecification[k] !== '[object Object]')
r[k] = allProductSpecification[k];
return r;
},{});
console.log(result);
&#13;
答案 1 :(得分:1)
如果你有一个JSON字符串(不是你发布的javascript对象文字的例子)。然后你可以使用JSON.parse
const json = `{
"Vinyl_Backlite": [{
"Price": "280",
"Quantity": "1",
"Amount": "280"
}],
"Steel": [{
"Price": "18",
"Quantity": "1",
"Amount": "18"
}],
"0": "[object Object]",
"1": "[object Object]"
}`;
const obj = JSON.parse(json, (key, value) => (value === {}.toString() ? undefined : value))
console.log(obj)