所以我有一个包含多个对象的对象数组,我想从对象中删除日期键,即createdDate和modifiedDate
我尝试过使用for循环并使用delete函数,但是什么也没发生。
var data = matchScoreData.tbl_FallOfWicket;
matchScoreData.tbl_FallOfWicket.forEach(element => {
if (matchScoreData.tbl_FallOfWicket.hasOwnProperty("createdDate")) {
delete ["createdDate"];
}
});
console.table(data);
var matchScoreData = {
"tbl_FallOfWicket": [
{
"pk_fallWicketID": 119,
"fk_matchID": 133137,
"fk_teamID": 141065,
"fk_dismissTypeID": 3,
"fk_dismissPlayerID": 969155,
"run": 40,
"wicket": 1,
"over": "5.1",
"inning": 1,
"createdDate": "2018-08-18T03:06:12+0000",
"modifiedDate": "",
},
{
"pk_fallWicketID": 120,
"fk_matchID": 133137,
"fk_teamID": 141065,
"fk_dismissTypeID": 1,
"fk_dismissPlayerID": 939721,
"run": 88,
"wicket": 2,
"over": "9.5",
"createdDate": "2018-08-18T03:06:12+0000",
"modifiedDate": "",
"inning": 1
}
]};
我在输出中得到相同的值
答案 0 :(得分:1)
您的问题是您正在使用window.rootViewController = HostingController(rootView: ContentView())
,它应该是delete["createdDate"]
,以便它可以从迭代的delete element["createdDate"]
中删除正确的属性。
element
注意:
您的代码还有一个更大的问题
matchScoreData.tbl_FallOfWicket.forEach(element => {
if (element.hasOwnProperty("createdDate")) {
delete element["createdDate"];
}
});
if (matchScoreData.tbl_FallOfWicket.hasOwnProperty("createdDate"))
是一个数组,因此此条件将始终返回false。因此,matchScoreData.tbl_FallOfWicket
永远不会执行。
您将不得不检查单个对象。
delete['...']
演示:
if (element.hasOwnProperty("createdDate"))
答案 1 :(得分:1)
以下工作,在这里,如果您的条件是指原始对象,但是您应该使用当前元素,在这种情况下,您需要使用 删除element [“ yourkey”]。
下面是适合您的JS。
var data = matchScoreData.tbl_FallOfWicket;
matchScoreData.tbl_FallOfWicket.forEach(element => {
if (element.hasOwnProperty("createdDate")) {
delete element["createdDate"];
}
});
console.table(data);
答案 2 :(得分:0)
必须为delete element.createDate
(与其他任何属性相同)
var matchScoreData = {
"tbl_FallOfWicket": [{
"pk_fallWicketID": 119,
"fk_matchID": 133137,
"fk_teamID": 141065,
"fk_dismissTypeID": 3,
"fk_dismissPlayerID": 969155,
"run": 40,
"wicket": 1,
"over": "5.1",
"inning": 1,
"createdDate": "2018-08-18T03:06:12+0000",
"modifiedDate": "",
},
{
"pk_fallWicketID": 120,
"fk_matchID": 133137,
"fk_teamID": 141065,
"fk_dismissTypeID": 1,
"fk_dismissPlayerID": 939721,
"run": 88,
"wicket": 2,
"over": "9.5",
"createdDate": "2018-08-18T03:06:12+0000",
"modifiedDate": "",
"inning": 1
}
]
};
var data = matchScoreData.tbl_FallOfWicket;
matchScoreData.tbl_FallOfWicket.forEach(element => {
if (element.hasOwnProperty("createdDate")) {
delete element.createdDate;
}
});
console.log(data);
答案 3 :(得分:0)
这有效
var matchScoreData = {
tbl_FallOfWicket: [
{
pk_fallWicketID: 119,
fk_matchID: 133137,
fk_teamID: 141065,
fk_dismissTypeID: 3,
fk_dismissPlayerID: 969155,
run: 40,
wicket: 1,
over: "5.1",
inning: 1,
createdDate: "2018-08-18T03:06:12+0000",
modifiedDate: ""
},
{
pk_fallWicketID: 120,
fk_matchID: 133137,
fk_teamID: 141065,
fk_dismissTypeID: 1,
fk_dismissPlayerID: 939721,
run: 88,
wicket: 2,
over: "9.5",
createdDate: "2018-08-18T03:06:12+0000",
modifiedDate: "",
inning: 1
}
]
};
for(let i=0;i<matchScoreData.tbl_FallOfWicket.length;i++){
delete matchScoreData.tbl_FallOfWicket[i].createdDate;
delete matchScoreData.tbl_FallOfWicket[i].modifiedDate;
}
console.log(matchScoreData);
答案 4 :(得分:0)
由于您始终将输出分配给新变量,因此可以使用map
和一些destructuring。
var matchScoreData = {"tbl_FallOfWicket":[{"pk_fallWicketID":119,"fk_matchID":133137,"fk_teamID":141065,"fk_dismissTypeID":3,"fk_dismissPlayerID":969155,"run":40,"wicket":1,"over":"5.1","inning":1,"createdDate":"2018-08-18T03:06:12+0000","modifiedDate":""},{"pk_fallWicketID":120,"fk_matchID":133137,"fk_teamID":141065,"fk_dismissTypeID":1,"fk_dismissPlayerID":939721,"run":88,"wicket":2,"over":"9.5","createdDate":"2018-08-18T03:06:12+0000","modifiedDate":"","inning":1}]};
const data = matchScoreData.tbl_FallOfWicket.map(obj => {
// pluck out the dates and assign the rest of the properties
// to "rest", then simply return those properties
const { createdDate, modifiedDate, ...rest } = obj;
return rest;
});
console.log(data);