3

I have JSON object with array pairs, like this:

Object {x: Array[36159], y: Array[36159], year: Array[36159]}

Small sample as example:

{
    "x": [309339618361.619, 102862032084.12102, 72892839276.09761, 46463392384.54194, -39360182208.042145],
    "y": [13950252.45052416, 1158787.402385158, -4368238.261400843, 1339913.7393283844, 2085336.6277048483],
    "year": [1991, 1991, 1992, 1992, 1992]
}

I want to filter this object on year. If JSON data was array or key, value pairs, I could have used filter:

data.filter(function (x) {
    return x.year == 1991
});

but as JSON structure is pairs of flat arrays, I have no idea how to approach.


Desired results is this:

{
    "x": [309339618361.619, 102862032084.12102],
    "y": [13950252.45052416, 1158787.402385158],
    "year": [1991, 1991]
}
vedar
  • 483
  • 8
  • 15
  • 2
    The fact that you received the data as JSON is irrelevant. Presumably you parsed the data into a JavaScript object, so that's what you are working with (see [What is the difference between JSON and Object Literal Notation?](http://stackoverflow.com/a/2904181/218196)) – Felix Kling Jul 02 '16 at 14:50
  • 3
    So what is desired result in this case? – Nenad Vracar Jul 02 '16 at 14:52
  • @NenadVracar I just updated my question – vedar Jul 02 '16 at 14:54
  • `filter()` is an array method but there is no outer array shown – charlietfl Jul 02 '16 at 14:55
  • `data.year.filter(function (x) { return x === 1991; });` ~~ that's what you want to do, right? –  Jul 02 '16 at 14:58
  • @KlaiderKlai that returns just years array, while I want to filter whole object on specific year – vedar Jul 02 '16 at 14:59

3 Answers3

2

You could use a callback with the comparison and an array for the properties, you want apply the filter on.

var object = {
        "x": [309339618361.619, 102862032084.12102, 72892839276.09761, 46463392384.54194, -39360182208.042145],
        "y": [13950252.45052416, 1158787.402385158, -4368238.261400843, 1339913.7393283844, 2085336.6277048483],
        "year": [1991, 1991, 1992, 1992, 1992]
    },
    result = {};

['x', 'y', 'year'].forEach(function (p) {
    result[p] = object[p].filter(function (a, i) {
        return object.year[i] === 1991;
    });
});

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You can use reduce() and add values by index of year that you want to filter by.

var obj = {
  "x": [309339618361.619, 102862032084.12102, 72892839276.09761, 46463392384.54194, -39360182208.042145],
  "y": [13950252.45052416, 1158787.402385158, -4368238.261400843, 1339913.7393283844, 2085336.6277048483],
  "year": [1991, 1991, 1992, 1992, 1992]
}

var result = obj.year.reduce(function(r, e, i) {
  if (e == 1991) {
    r.x = (r.x || []).concat(obj.x[i]);
    r.y = (r.y || []).concat(obj.y[i]);
    r.year = (r.year || []).concat(obj.year[i])
  }
  return r;
}, {});

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • I'm in doubt which answer to accept, so I decided to choose Nina's as she replied first. Thanks – vedar Jul 02 '16 at 15:17
  • 1
    without the check for every loop and while push is optimised for one element and concat for more and concat returns the whole array with the new items and the needed assignment. i go for push. – Nina Scholz Jul 02 '16 at 15:26
0

Simply do a copy of obj and iterate the elements of the year property and remove which aren't equal to 1991.

var obj = {

    x:    [309339618361.619, 102862032084.12102],
    y:    [13950252.45052416, 1158787.402385158],
    year: [1991, 1991]

},

    scan = {};

Object.assign(scan, obj);

for(var i = 0, len = scan.year.length; i < len; i ++) {

    if(scan.year[i] !== 1991) {

        scan.year.splice(i, 1);

    }

}

console.log(scan);