如何从json对象中删除索引值

时间:2019-02-18 19:02:16

标签: node.js json

如何从json对象中删除数组索引值

我尝试使用stringify,但是无法获得预期的结果。

self.locationManager.startMonitoringSignificantLocationChanges()

我需要将其转换为

{
  "header":{"test":"test1", "test2":"test2"},
  "results_1":{"a":"b"},
  "results_2":{"0":{"id1":"1", "value1" :1}, "1":{"id2":"2", "value2":2 }}
}

1 个答案:

答案 0 :(得分:3)

使用替换功能:

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; }
}));