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

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

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

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

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

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.