gpt4 book ai didi

javascript - 对象数组,过滤掉带 0 的值

转载 作者:行者123 更新时间:2023-11-30 09:39:45 24 4
gpt4 key购买 nike

我想删除值为 0 的对象的 key + value。时间戳应该保留在原位。

var array = [{
"timestamp": "2017-01-18T00:00:00.000Z",
"Test Bar 0": 78.09482851766388
}, {
"timestamp": "2017-01-18T01:00:00.000Z",
"Test Bar 0": 124.09589189108233
}, {
"timestamp": "2017-01-18T02:00:00.000Z",
"Test Bar 0": 106.97921714748477
}, {
"timestamp": "2017-01-18T03:00:00.000Z",
"Test Bar 0": 118.7469310337081
}, {
"timestamp": "2017-01-18T04:00:00.000Z",
"Test Bar 0": 119.81672320518294
}, {
"timestamp": "2017-01-18T05:00:00.000Z",
"Test Bar 0": 67.39690680291541
}, {
"timestamp": "2017-01-18T06:00:00.000Z",
"Test Bar 0": 117.67713886223325
}, {
"timestamp": "2017-01-18T07:00:00.000Z",
"Test Bar 0": 87.72295806093751
}, {
"timestamp": "2017-01-18T08:00:00.000Z",
"Test Bar 0": 115.53755451928356
}, {
"timestamp": "2017-01-18T09:00:00.000Z",
"Test Bar 0": 78.09482851766388
}, {
"timestamp": "2017-01-18T10:00:00.000Z",
"Test Bar 0": 48.14064771636815
}, {
"timestamp": "2017-01-18T11:00:00.000Z",
"Test Bar 0": 67.39690680291541
}, {
"timestamp": "2017-01-18T12:00:00.000Z",
"Test Bar 0": 130.5146449199314
}, {
"timestamp": "2017-01-18T13:00:00.000Z",
"Test Bar 0": 128.37506057698172
}, {
"timestamp": "2017-01-18T14:00:00.000Z",
"Test Bar 0": 73.81565983176449
}, {
"timestamp": "2017-01-18T15:00:00.000Z",
"Test Bar 0": 109.11880149043446
}, {
"timestamp": "2017-01-18T16:00:00.000Z",
"Test Bar 0": 127.30526840550688
}, {
"timestamp": "2017-01-18T17:00:00.000Z",
"Test Bar 0": 123.02609971960749
}, {
"timestamp": "2017-01-18T18:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-18T19:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-18T20:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-18T21:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-18T22:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-18T23:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T00:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T01:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T02:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T03:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T04:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T05:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T06:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T07:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T08:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T09:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T10:00:00.000Z",
"Test Bar 0": 0
}, {
"timestamp": "2017-01-19T11:00:00.000Z",
"Test Bar 0": 0
}]


var abc = array.filter((object) => {
let keys = []
for (let key in object) {
if (key !== 'timestamp') keys.push(key)
}
return keys.map(key => object[key] > 0)
})

console.log(abc)

但是我的代码没有删除 0 个值

最佳答案

你的 keys.map 是错误的:

var abc = array.forEach((object) => {
for(key in object){
if(key!=="timestamp"&&(object[key]==0||object[key]=="0") {
delete object[key];
}
}
});

关于javascript - 对象数组,过滤掉带 0 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726385/

24 4 0
文章推荐: javascript - 如何为动态生成的 div 创建 onClick 函数
文章推荐: javascript - 在 PouchDB 中记住登录数据
文章推荐: javascript - 获取 promise - 返回 500 内部服务器错误
文章推荐: javascript - 如何将 <script></script> 附加到
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com