1

I need to get the timestamp of the object where new_name in status_change is Solved.

I have tried this.

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

But above code didn't give the expected result.

Here is my data set.

{
 "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"
 }
 }
 ]
}

What could be the issue? JSFiddle

asked Nov 15, 2018 at 7:11

6 Answers 6

1

You can use more than one boolean in find() to short-circuit it so you don't need the repeated find() calls:

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

answered Nov 15, 2018 at 7:16
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the quick response and help me to reduce the repeated find() calls
1

You can first use .filter() to filter out the not required objects. And then use .map() to get the necessary fields.

Here is how:

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

answered Nov 15, 2018 at 7:18

Comments

1

Well instead of using .find() in your conditions, you better use .some() which is made for this, and also you can use only one .some() call to group all of these conditions.

And instead of writing x.status_change != null you can just write x.status_change, which gives the same result.

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

Demo:

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

answered Nov 15, 2018 at 7:26

3 Comments

Thanks for your answer. Btw, this gives wrong result. Expected result is 2018年04月27日 00:10:09
@Bishan Sorry my fault, the condition was missing.
After the update, Now it's giving the correct result. Thanks for pointing out the null check.
0

this should work

  • filtering the existing status_change
  • filtering new_name === "Solved"
  • mapping timestamps

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

answered Nov 15, 2018 at 7:23

Comments

0

The code below will do what you have asked. solvedData will contain timestamp of the objects, in which status_change is Solved. See code below. Feel free for any question.

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

answered Nov 15, 2018 at 7:20

Comments

0

Perhaps the most stupid, but IMHO the simplest solution:

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);
 }
 }
}
answered Nov 15, 2018 at 8:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.