gpt4 book ai didi

javascript - 在 Javascript 中过滤对象

转载 作者:行者123 更新时间:2023-11-28 11:07:05 25 4
gpt4 key购买 nike

是否有快速方法来过滤对象数组以仅返回每个对象中的几个属性?

例如我们有以下数据:

var objArr = [{

"Title": "July 13 - July 19 2014",
"displayAd_imp": "3,500",
"videoAd_imp": "1.5",
"tv_imp": "0.52",
"Date": "2014-07-17T00:00:00.000Z",
"WeekNo": 29
}, {

"Title": "July 20 - July 26 2014",
"displayAd_imp": "1,600",
"videoAd_imp": "2.55",
"tv_imp": "0.052",
"Date": "2014-07-24T00:00:00.000Z",
"WeekNo": 30
}, {

"Title": "July 27 - Aug 2 2014",
"displayAd_imp": "1,500",
"videoAd_imp": "2.1",
"tv_imp": "0.122",
"Date": "2014-07-31T00:00:00.000Z",
"WeekNo": 31
}]

我正在尝试过滤上面的数组以获得另一个仅包含 videoAd_impvideoAd_imptv_imp 的数组。所以它看起来像这样:

 [{

"displayAd_imp": "3,500",
"videoAd_imp": "1.5",
"tv_imp": "0.52",
}, {

"displayAd_imp": "1,600",
"videoAd_imp": "2.55",
"tv_imp": "0.052",

}, {

"displayAd_imp": "1,500",
"videoAd_imp": "2.1",
"tv_imp": "0.122",
}]

提前致谢!

最佳答案

使用Array.map就像下面这样

如果您想要对所有对象的新引用。

var newArr = objArr.map(function (obj) {
return {displayAd_imp:obj.displayAd_imp,videoAd_imp:obj.videoAd_imp,tv_imp:obj.tv_imp};
})

console.log(newArr);

如果你想获得原始引用文献,请使用如下

var newArr = objArr.map(function (obj) {
delete obj.Date;
delete obj.WeekNo;
delete obj.Title
return obj;
})

关于javascript - 在 Javascript 中过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25554867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com