Reputation: 1605
Let say We have the following json object:
[{"a1" : "a1Val", "a2" : "a2Val", "a3" : "a3Val"},
{"b1" : "b1Val", "b2" : "b2Val", "b3" : "b3Val"},
....
{"z1" : "z1Val", "z2" : "z2Val", "z3" : "z3Val"}]
How can we retrieve from this object array of only X2 key value pairs.
meaning, the following result:
[ {"a2" : "a2Val"}, {"b2" : "b2Val"}, ... ,{ "z2" : "z2Val"}]
within the best performance.
The key does not have to contains number.
What I need here is a function that will recieve parameter - i and return an array of all the i-th objects in the original json object
So for example if we look at the above json object and we invoke the method we 2 the method will return
[ {"a2" : "a2Val"}, {"b2" : "b2Val"}, ... ,{ "z2" : "z2Val"}]
Hope it's clear enough.
Upvotes: 0
Views: 26979
Reputation: 210
var initialArr = [
{"a1" : "a1Val", "a2" : "a2Val", "a3" : "a3Val"},
{"b1" : "b1Val", "b2" : "b2Val", "b3" : "b3Val"},
{"z1" : "z1Val", "z2" : "z2Val", "z3" : "z3Val"}
];
var resArr = initialArr.map(function(x){
return x[Object.keys(x)[1]];
});
Here in this resArr, I am mapping it to initialArr[ ], to generate an array out of it called resArr and the each element initialArr is represented as 'x' in this x is an object now inorde to get the 2nd element of the object x[1] so here, 1 represents the second element as index starts from 0,1 ,,,so we need to get the keys of that object by Object.keys(x)[1]...hence value would be x[Object.keys(x)[1]].
Upvotes: 3
Reputation: 23482
You will probably find that it is more efficient to do this when parsing your JSON string by using a reviver function. Something like this.
var json = '[{"a1" : "a1Val", "a2" : "a2Val", "a3" : "a3Val"},{"b1" : "b1Val", "b2" : "b2Val", "b3" : "b3Val"},{"z1" : "z1Val", "z2" : "z2Val", "z3" : "z3Val"}]',
data = JSON.parse(json, function (key, value) {
if (key !== "" && typeof value === 'object') {
Object.keys(value).forEach(function (name) {
if (name.slice(-1) !== '2') {
delete value[name];
}
});
}
return value;
});
document.body.appendChild(document.createTextNode(JSON.stringify(data)));
Upvotes: 1
Reputation: 31949
Just a really fancy way to do exactly what you asked. Applicable to anything after little tweaking
var json = '[{"a1" : "a1Val", "a2" : "a2Val", "a3" : "a3Val"},{"b1" : "b1Val", "b2" : "b2Val", "b3" : "b3Val"},{"z1" : "z1Val", "z2" : "z2Val", "z3" : "z3Val"}]',
parsed = JSON.parse(json),
index = 2, // here goes your value
result = parsed.map(function(obj){
var key = Object.keys(obj)[0][0]+index,
retDict = {};
return retDict[key] = obj[key], retDict;
});
Run this in a console and see.
Upvotes: 1
Reputation: 687
What about this:
var collection, key, obj, objArr, _i, _len, _tmp;
collection = [
{
"a1": "a1Val",
"a2": "a2Val",
"a3": "a3Val"
}, {
"b1": "b1Val",
"b2": "b2Val",
"b3": "b3Val"
}
];
objArr = [];
for (_i = 0, _len = collection.length; _i < _len; _i++) {
obj = collection[_i];
for (key in obj) {
_tmp = {};
_tmp[key] = obj[key];
objArr.push(_tmp);
}
}
Upvotes: 0
Reputation: 59232
You could use Array.map
here
var arr = oldArr.map(function(obj){
var key = Object.keys(obj).sort()[1], rtn = {};
return rtn[key] = obj[key], rtn;
});
What you're doing is taking the second key and then returning a new Object with that key and value.
Upvotes: 9
Reputation: 7336
Convert to array, map the keys, filter results, concat it all, etc. This should work for you:
var result = arr
.map(function (obj) {
return Object.keys(obj).map(function (key) {
var result = {};
result[key] = obj[key];
if (key.substring(1) === "2") {
return result;
}
});
})
.reduce(function (x, y) { return x.concat(y); }, [])
.filter(function (obj) { return obj });
Upvotes: 0
Reputation: 63
Do a forEach loop on the array. Then for each value get the key with a regular expression. (Loop through object get value using regex key matches Javascript). I won't give you the full code because it is easy to implement and it is a good learning experience.
Upvotes: 0