我需要获取timestamp
中new_name
已已解决的对象的status_change
。
我已经尝试过了。
console.log(
ticket.updates ?
(
(ticket.updates.find(x => x.status_change !== null) &&
ticket.updates.find(x => x.status_change !== null).status_change.new_name === 'Solved') ?
ticket.updates.find(x => x.status_change !== null).timestamp :
'new_name is ' + ticket.updates.find(x => x.status_change !== null).status_change.new_name
)
: 'No updates');
但是上面的代码没有给出预期的结果。
这是我的数据集。
{
"updates": [{
"timestamp": "2018-04-26 06:39:12",
"by": {
"name": "A1"
},
"status_change": {
"new_name": "Open",
"old_name": null
}
}, {
"timestamp": "2018-04-27 00:09:44",
"by": {
"name": "B1"
},
"status_change": null
}, {
"timestamp": "2018-04-27 00:10:09",
"by": {
"name": "B1"
},
"status_change": {
"new_name": "Solved",
"old_name": "Open"
}
}
]
}
可能是什么问题? JSFiddle
答案 0 :(得分:1)
您可以在find()
中使用多个布尔来使其短路,因此您不需要重复的find()
调用:
let obj = {"updates": [{"timestamp": "2018-04-26 06:39:12","by": {"name": "A1"},"status_change": {"new_name": "Open","old_name": null}}, {"timestamp": "2018-04-27 00:09:44","by": {"name": "B1"},"status_change": null}, {"timestamp": "2018-04-27 00:10:09","by": {"name": "B1"},"status_change": {"new_name": "Solved","old_name": "Open"}}]}
let item = obj.updates.find(item =>
item.status_change
&& item.status_change.new_name == "Solved")
if (item) { // found one
console.log(item.timestamp)
} else { // not found
console.log("no new items")
}
答案 1 :(得分:1)
您可以首先使用.filter()
过滤掉不需要的对象。然后使用.map()
获取必要的字段。
方法如下:
const data = {
"updates": [{
"timestamp": "2018-04-26 06:39:12",
"by": {
"name": "A1"
},
"status_change": {
"new_name": "Open",
"old_name": null
}
}, {
"timestamp": "2018-04-27 00:09:44",
"by": {
"name": "B1"
},
"status_change": null
}, {
"timestamp": "2018-04-27 00:10:09",
"by": {
"name": "B1"
},
"status_change": {
"new_name": "Solved",
"old_name": "Open"
}
}
]
}
const timestamps = data.updates
.filter(x => x.status_change && x.status_change.new_name === 'Solved')
.map(x => x.timestamp);
if(timestamps.length >= 1) {
console.log('updates:', timestamps);
} else {
console.log('no updates');
}
答案 2 :(得分:1)
好吧,不要在条件中使用.find()
,而最好使用为此目的而设计的.some()
,并且也只能使用一个.some()
调用来对所有这些条件进行分组。
除了编写x.status_change != null
之外,您还可以编写x.status_change
来得到相同的结果。
console.log(
ticket.updates ?
ticket.updates.some(x => x.status_change && x.status_change.new_name === 'Solved') ?
ticket.updates.find(x => x.status_change && x.status_change.new_name === 'Solved').timestamp :
'new_name is ' + ticket.updates.find(x => x.status_change).status_change.new_name
: 'No updates');
演示:
let ticket = {
"updates": [{
"timestamp": "2018-04-26 06:39:12",
"by": {
"name": "A1"
},
"status_change": {
"new_name": "Open",
"old_name": null
}
}, {
"timestamp": "2018-04-27 00:09:44",
"by": {
"name": "B1"
},
"status_change": null
}, {
"timestamp": "2018-04-27 00:10:09",
"by": {
"name": "B1"
},
"status_change": {
"new_name": "Solved",
"old_name": "Open"
}
}]
};
console.log(
ticket.updates ?
ticket.updates.some(x => x.status_change && x.status_change.new_name === 'Solved') ?
ticket.updates.find(x => x.status_change && x.status_change.new_name === 'Solved').timestamp :
'new_name is ' + ticket.updates.find(x => x.status_change).status_change.new_name
: 'No updates');
答案 3 :(得分:0)
下面的代码将满足您的要求。 solvedData
将包含对象的时间戳,其中status_change
已已解决。请参见下面的代码。随时解决任何问题。
var myData = {
"updates": [{
"timestamp": "2018-04-26 06:39:12",
"by": {
"name": "A1"
},
"status_change": {
"new_name": "Open",
"old_name": null
}
}, {
"timestamp": "2018-04-27 00:09:44",
"by": {
"name": "B1"
},
"status_change": null
}, {
"timestamp": "2018-04-27 00:10:09",
"by": {
"name": "B1"
},
"status_change": {
"new_name": "Solved",
"old_name": "Open"
}
}
]
};
var solvedData = [];
var i=0;
if(myData.updates){
for(i=0;i<myData.updates.length; i++){
if(myData.updates[i].status_change && myData.updates[i].status_change.new_name === "Solved"){
solvedData.push(myData.updates[i].timestamp);
}
}
}
for(i=0;i<solvedData.length; i++){
console.log(solvedData[i]);
}
答案 4 :(得分:0)
这应该有效
const tickets = {
"updates": [{
"timestamp": "2018-04-26 06:39:12",
"by": {
"name": "A1"
},
"status_change": {
"new_name": "Open",
"old_name": null
}
}, {
"timestamp": "2018-04-27 00:09:44",
"by": {
"name": "B1"
},
"status_change": null
}, {
"timestamp": "2018-04-27 00:10:09",
"by": {
"name": "B1"
},
"status_change": {
"new_name": "Solved",
"old_name": "Open"
}
}]
};
console.log(tickets.updates
.filter(x => x.status_change && x.status_change.new_name === "Solved")
.map(x => x.timestamp));
答案 5 :(得分:0)
也许是最愚蠢的,但恕我直言是最简单的解决方案:
var ticket = {"updates":[{"timestamp":"2018-04-26 06:39:12","by":{"name":"A1"},"status_change":{"new_name":"Open","old_name":null}},{"timestamp":"2018-04-27 00:09:44","by":{"name":"B1"},"status_change":null},{"timestamp":"2018-04-27 00:10:09","by":{"name":"B1"},"status_change":{"new_name":"Solved","old_name":"Open"}}]};
for (var i in ticket.updates ) {
var item=ticket.updates[i];
if (typeof item.status_change === 'object' && item.status_change !== null) {
if (item.status_change.new_name === 'Solved') {
alert('Found timestamp value: ' + item.timestamp);
}
}
}