0

I have a json object (much larger):

data: {
pistonNoiseColdIdle: 6.5,
pistonNoiseColdSnap: 6.5,
pistonNoiseColdSweep: 6.5,
pistonNoiseHotIdle: 6.5,
pistonNoiseHotSnap: 6.5
}

And I want to keep only what's in a second array:

["pistonNoiseColdIdle", "pistonNoiseColdSnap", "pistonNoiseColdSweep", "pistonNoiseHotIdle", "pistonNoiseHotSnap"]

How do I output an object by filtering through the large one?

Kevin Brown
  • 12,602
  • 34
  • 95
  • 155
  • Possible Duplicate: http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list – Duane Aug 15 '14 at 14:11

3 Answers3

1

You can use Object.keys() method ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys )

var obj = { data: {
pistonNoiseColdIdle: 6.5,
pistonNoiseColdSnap: 6.5,
pistonNoiseColdSweep: 6.5,
pistonNoiseHotIdle: 6.5,
pistonNoiseHotSnap: 6.5
} };

var myArray = Object.keys(obj.data);

edited after comment:

If you want to create a new object with the keys specified in your array, maybe you can try iterating the array?

var obj = { data: {
pistonNoiseColdIdle: 6.5,
pistonNoiseColdSnap: 6.5,
pistonNoiseColdSweep: 4.5,
pistonNoiseHotIdle: 6.5,
pistonNoiseHotSnap: 1.5
} };

var myArray = ["pistonNoiseColdIdle", "pistonNoiseColdSnap", "pistonNoiseColdSweep", "pistonNoiseHotIdle", "pistonNoiseHotSnap"];

var filteredObj = {};
for(i=0;i<myArray.length;i++) {
    key = myArray[i];
    filteredObj[key] = obj.data[key];
}

// var filteredObj will contain only the keys in "myArray"
lpg
  • 4,897
  • 1
  • 16
  • 16
0

One simple way would be to just loop over the second array and add relevant properties to a new object:

var obj = {data: {
    pistonNoiseColdIdle: 6.5,
    pistonNoiseColdSnap: 6.5,
    pistonNoiseColdSweep: 6.5,
    pistonNoiseHotIdle: 6.5,
    pistonNoiseHotSnap: 6.5
}};
var props = ["pistonNoiseColdIdle", "pistonNoiseColdSnap", "pistonNoiseColdSweep", "pistonNoiseHotIdle", "pistonNoiseHotSnap"];

var result = new Object();
$.each(props, function(item) {
    if(obj.data.hasOwnProperty(item)) {
        result[item] = obj.data[item];
    }
});

//now result object contains what you need
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • The result object is empty? I'm looking for result to be `obj` with the only remaining keys to be what's in the props. ie, if props contains only three attributes, the result object should have the matching key/value pair. – Kevin Brown Aug 15 '14 at 14:21
0

Edit, updated

var arr = ["pistonNoiseColdIdle"
          , "pistonNoiseColdSnap"
          , "pistonNoiseColdSweep"
          , "pistonNoiseHotIdle"
          , "pistonNoiseHotSnap"];

var data = {
pistonNoiseColdIdle: 6.5,
pistonNoiseColdSnap: 6.5,
pistonNoiseColdSweep: 6.5,
pistonNoiseHotIdle: 6.5,
pistonNoiseHotSnap: 6.5,
other : 0
};

var _data = {};

$.map(arr, function(v, k) {
    return data[v] != undefined ? _data[v] = data[v] : null
}); 

console.log(_data);

jsfiddle http://jsfiddle.net/guest271314/shfe43fs/

guest271314
  • 1
  • 15
  • 104
  • 177
  • Please see above comments. I need to match the array to the object and keep the matching key/value pair in the data object. – Kevin Brown Aug 15 '14 at 15:02
  • If interpret accurately , map the `data` object to an `array` ? Or , find properties within the `data` object which match the "And I want to keep only what's in a second array" ? If possible , please clarify ? Thanks – guest271314 Aug 15 '14 at 15:07
  • I have an object and an array. I want to keep the key/value pair of the object based on the values in the array. So, if the object has 30 key/values and the array has the ones mentioned above, I want the resulting object to only contain the key/value corresponding to the array. – Kevin Brown Aug 15 '14 at 15:12