如何从json对象中删除索引值
如何从json对象中删除数组索引值
我曾尝试使用stringify但无法获得预期的结果。
{
"header":{"test":"test1", "test2":"test2"},
"results_1":{"a":"b"},
"results_2":{"0":{"id1":"1", "value1" :1}, "1":{"id2":"2", "value2":2 }}
}
我需要将其转换为
{
"header":{"test":"test1", "test2":"test2"},
"results_1":{"a":"b"},
"results_2":[{"id1":"1", value:1}, {"id2":"2", value:2}]
}
回答如下:
使用替换函数:
const o = {
"header":{"test":"test1", "test2":"test2"},
"results_1":{"a":"b"},
"results_2":{"0":{"id1":"1", "value1" :1}, "1":{"id2":"2", "value2":2 }}
};
console.log(JSON.stringify(o, (k, v) => {
if (
typeof v == 'object' &&
!Array.isArray(v) &&
v.hasOwnProperty(0)
) { return Object.values(v); }
else { return v; }
}));