3

I have a JavaScript array of objects that has ISO 8601 date strings as keys, and a number for the value like this:

var myArray = [
  {"Wed May 27 2015 04:46:04 GMT+0000 (UTC)":1.1},
  {"Wed May 24 2015 10:34:04 GMT+0000 (UTC)":2.2}
]

I want to find and remove any objects that match a certain date like this:

var myDate = "Wed May 27 2015 04:46:04 GMT+0000 (UTC)"

if(myDate is a key in myArray){
  //Remove it from myArray
}

And the result would be that myArray now looks like this:

[{"Wed May 24 2015 10:34:04 GMT+0000 (UTC)":2.2}]

I have Underscore JS available, but I can't figure this out.

3
  • Dates aren't ISO 8601 strings in your code snippets. Commented May 27, 2015 at 6:09
  • Hmm... are they the wrong format? I'm using Parse, and this is the format it returns and they claim their Date data type is ISO 8601. Commented May 27, 2015 at 7:10
  • 1
    ISO 8601 Universal datetime template: YYYY-MM-ddThh:mm:ssZ. new Date().toJSON() returns current date/time in ISO 8601. But new Date().toString() returns current date/time in human format like in your code snippets. Commented May 27, 2015 at 7:18

2 Answers 2

3

You can use _.filter, _.has, _.partial and _.negate methods, like this

console.log(_.filter(myArray, _.partial(_.negate(_.has), _, myDate)));
// [ { 'Wed May 24 2015 10:34:04 GMT+0000 (UTC)': 2.2 } ]
  • Here, _.negate(_.has) will return a new function which will just negate the result of _.has always.

  • _.partial(_.negate(_.has), _, myDate) will return a new function which will accept the values from myArray in the placeholder _ and _.has will be invoked with the value from the myArray and myDate as arguments.

  • _.has will return true if the object passed as the first argument has a key with the same name as the second parameter passed to it.


For older version of _

If you are using an older version of _, then you can simply pass a function object to do this,

console.log(_.filter(myArray, function(currentObject) {
    return !_.has(currentObject, myDate);
}));
// [ { 'Wed May 24 2015 10:34:04 GMT+0000 (UTC)': 2.2 } ]

Vanila JS Version

console.log(myArray.filter(function(currenObject) {
    return !currenObject.hasOwnProperty(myDate);
}));
// [ { 'Wed May 24 2015 10:34:04 GMT+0000 (UTC)': 2.2 } ]

You can simply use Array.prototype.filter function and check if the current object has a property with the value of myDate, with Object.prototype.hasOwnProperty.

If you are sure that the myDate will not have a value which will not be one of the inherited properties of Object, then you can also use in operator, like this

console.log(myArray.filter(function(currenObject) {
    return !(myDate in currenObject);
}));
// [ { 'Wed May 24 2015 10:34:04 GMT+0000 (UTC)': 2.2 } ]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help. :) I had to use the filter version since I have an older version of Underscore. The only catch is that your solution is returning the value matching myDate instead of removing that value from the array.
You can assign the result to myArray. The advantage here is we don't mutate the original object
I've done that, but again, it's having the opposite effect. It's returning an array with myDate in it instead of an array without myDate in it.
@CliftonLabrum Okay, I updated the original _ version with _.negate and other versions with the native logical not operator (!). Please check now.
1

I don't know about the underscoreJs, but I hope this simple way will help you out.

var myArray = [
              {"Wed May 27 2015 04:46:04 GMT+0000 (UTC)":1.1},
              {"Wed May 24 2015 10:34:04 GMT+0000 (UTC)":2.2}
            ];
var myDate = "Wed May 27 2015 04:46:04 GMT+0000 (UTC)";

for(i=0;i<myArray.length;i++)
{
    if(myDate == Object.keys(myArray[i]))
    {
        myArray.splice(i,1);
    }
}

Comments

Your Answer

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.