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' },
];
njzk2
  • 38,969
  • 7
  • 69
  • 107
Kay Singian
  • 1,301
  • 8
  • 20
  • 33
  • 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"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Sep 24 '13 at 15:11

8 Answers8

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);
});
techfoobar
  • 65,616
  • 14
  • 114
  • 135
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];
}
matewka
  • 9,912
  • 2
  • 32
  • 43
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.

Oriol
  • 274,082
  • 63
  • 437
  • 513
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.

dejavu
  • 3,236
  • 7
  • 35
  • 60
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]);
    }
}
Tho
  • 23,158
  • 6
  • 60
  • 47
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/

Richard Banks
  • 2,946
  • 5
  • 34
  • 71
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);
laserface
  • 449
  • 1
  • 4
  • 12
-3

You can do this with the jQuery extend function

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

var object = $.extend({}, object1, object2);
Paul Rad
  • 4,820
  • 1
  • 22
  • 23