0

I have two JavaScript objects:

object_1 = [
    {'value': '9:00', 'text':'9:00 am', 'eventtime':'09:00:00' },
    {'value': '9:30', 'text':'9:30 am', 'eventtime':'09:30:00' },
    {'value': '10:00', 'text':'10:00 am', 'eventtime':'10:00:00' },
    {'value': '10:30', 'text':'10:30 am', 'eventtime':'10:30:00' },
    {'value': '11:00', 'text':'11:00 am', 'eventtime':'11:00:00' },
    {'value': '11:30', 'text':'11:30 am', 'eventtime':'11:30:00' },
];
object_2 = [
    {'eventtime': '10:30:00'},
    {'eventtime': '11:00:00'}
];

I want to remove the object in object_1 which has the same eventtime value and store it in a new array/object . Please help me do so, I cant find a solution to this.

This will be the new array/object:

object_new = [

    {'value': '9:00', 'text':'9:00 am', 'eventtime':'09:00:00' },
    {'value': '9:30', 'text':'9:30 am', 'eventtime':'09:30:00' },
    {'value': '10:00', 'text':'10:00 am', 'eventtime':'10:00:00' },
    {'value': '11:30', 'text':'11:30 am', 'eventtime':'11:30:00' },
];
1
  • 1
    Please note that the problem has nothing to do with JSON at all. It seems you are confusing JavaScript object literals (constructs of the JavaScript language syntax) with JSON (a language-independent data-exchange format, like XML or CSV). I will edit your question accordingly. See also: There is no such thing as a "JSON object". Commented Sep 24, 2013 at 15:11

8 Answers 8

4

Here is one approach:

// create a plain array with just the eventtime values
var values = object_2.map(function(item) { return item['eventtime']; });

// use .filter() to get an array with just the values we need
var result = object_1.filter(function(item) {
    return !(values.indexOf(item['eventtime']) !== -1);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Cool, it's like my answer but simplified, thanks to new es5 array methods. But it should be noted that doesn't work on old browsers
You can accomplish the same thing using jQuery's $.map and $.grep
0

You can use

// Declare variables
var object_1 = [
    {'value': '9:00', 'text':'9:00 am', 'eventtime':'09:00:00' },
    {'value': '9:30', 'text':'9:30 am', 'eventtime':'09:30:00' },
    {'value': '10:00', 'text':'10:00 am', 'eventtime':'10:00:00' },
    {'value': '10:30', 'text':'10:30 am', 'eventtime':'10:30:00' },
    {'value': '11:00', 'text':'11:00 am', 'eventtime':'11:00:00' },
    {'value': '11:30', 'text':'11:30 am', 'eventtime':'11:30:00' }
],
object_2 = [
    {'eventtime': '10:30:00'},
    {'eventtime': '11:00:00'}
],
object_new = object_1.slice(),
temp = [];

// Obtain temp=['10:30:00','11:00:00'] from object_2
for (var i=object_2.length-1; i>=0; --i) {
    temp[i] = object_2[i].eventtime;
}

// Remove the elements you don't want
for (var i=object_new.length-1; i>=0; --i) {
    if ( temp.indexOf( object_new[i].eventtime ) !== -1) {
        object_new.splice(i, 1);
    }
}

Demo: http://jsfiddle.net/FfSjL/

Note that if first you convert [{'eventtime': '10:30:00'}, {'eventtime': '11:00:00'}] into ['10:30:00','11:00:00'], then you can use the native indexOf, which is much faster than a javascript loop.

Comments

0

Try this:

for (var i in object_2) {
    for (var j in object_1) {
        if (object_1[j].eventtime === object_2[i].eventtime) {
            delete object_1[j];
            break;
        }
    }
}

That solution above preserves the array keys but if you want to reset them you can do another for loop

var object_new = [];
for (var i in object_1) {
    object_new[object_new.length] = object_1[i];
}

Comments

0
for (var i = object_1.length - 1; i >= 0; i--)
{
    for (var j in object_2)
    {
        if (object_1[i].eventtime === object_2[j].eventtime)
        {
           object_1.splice(i, 1);
           break;
        }
    }
}

JSfiddle link : http://jsfiddle.net/androdify/5ukBF/

Note: It will modify object_1 array.

Comments

0

I think, the simple solution is (tested):

var object_new = [];
for (var idx1 in object_1) {
    var found = false;
    for (var idx2 in object_2) {
        if (object_1[idx1].eventtime === object_2[idx2].eventtime) {
            found = true;
            break;
        }
    }
    if(!found){
        object_new.push(object_1[idx1]);
    }
}

Comments

0

this seems to work ok

 object_1 = [
        {'value': '9:00', 'text':'9:00 am', 'eventtime':'09:00:00' },
        {'value': '9:30', 'text':'9:30 am', 'eventtime':'09:30:00' },
        {'value': '10:00', 'text':'10:00 am', 'eventtime':'10:00:00' },
        {'value': '10:30', 'text':'10:30 am', 'eventtime':'10:30:00' },
        {'value': '11:00', 'text':'11:00 am', 'eventtime':'11:00:00' },
        {'value': '11:30', 'text':'11:30 am', 'eventtime':'11:30:00' },
    ];

object_2 = [
    {'eventtime': '10:30:00'},
    {'eventtime': '11:00:00'}
];

temp = object_2.map(function(o){return o["eventtime"]; })

var object_new = jQuery.grep(object_1, function( n, i ) {
  return ( temp.indexOf(n.eventtime) === -1);
});

Heres a link to my fiddle

http://jsfiddle.net/ricobano/FfSjL/2/

Comments

0

I wanted to give a shot before I left for work, but I only got half of the way.

Here's a function that will give you all the eventtime values. Just call it on each object like so:

getVals(object_1);

function getVals(obj) {
    var vals = [];
    for (var i = 0, len = obj.length; i < len; i++) {
        for(prop in obj[i]) {
            if(prop === 'eventtime') {
                vals.push(obj[i][prop]);
            }
        }
    };
    return vals;
}

I hope that at least helps get you closer to your result.

Also, I just wanted to point out that there were a couple of syntax errors inside of your objects. I fixed them for you:

object_1 = [
    {'value': '9:00', 'text':'9:00 am', 'eventtime':'09:00:00' },
    {'value': '9:30', 'text':'9:30 am', 'eventtime':'09:30:00' },
    {'value': '10:00', 'text':'10:00 am', 'eventtime':'10:00:00' },
    {'value': '10:30', 'text':'10:30 am', 'eventtime':'10:30:00' },
    {'value': '11:00', 'text':'11:00 am', 'eventtime':'11:00:00' },
    {'value': '11:30', 'text':'11:30 am', 'eventtime':'11:30:00' }
];
object_2 = [
    {'eventtime': '10:30:00'},
    {'eventtime': '11:00:00'}
];

Combine that with @techfoobar 's really nice solution and it seems to work for me:

var values = object_2.map(function(item) {
    return item['eventtime'];
});

var result = object_1.filter(function(item) {
    if(values.indexOf(item['eventtime']) !== -1) {
        return false;
    }
    return true;
});

console.log(result);

Comments

-3

You can do this with the jQuery extend function

http://api.jquery.com/jQuery.extend/

var object = $.extend({}, object1, object2);

2 Comments

How would a merge remove object ?
This is a poor answer. Please dont just post links especially if it doesnt actually answer the question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.