Reputation: 2117
I have 3 json arrays, each with information listed in the same format:
Array:
ID:
NAME:
DATA:
ID:
NAME:
DATA:
etc...
My goal is to combine all 3 arrays into one array, and sort and display by NAME by passing the 3 arrays into a function.
The function I've tried is:
Javascript Call:
// to save time I'm just passing the name of the array, I've tried passing
// the full array name as json[0]['DATA'][array_1][0]['NAME'] as well.
combineNames(['array_1','array_2']);
FUNCTION:
function combineNames(names) {
var allNames = []
for (i=0;i<names.length;i++) {
for (j=0;j<json[0]['DATA'][names[i]].length;j++) {
allNames.push(json[0]['DATA'][names[i]][j]['NAME']);
}
}
return allNames.sort();
}
The above gives me the error that NAME is null or undefined.
I've also tried using the array.concat function which works when I hard code it:
var names = [];
var allNames = [];
var names = names.concat(json[0]['DATA']['array_1'],json[0]['DATA']['array_2']);
for (i=0;i<names.length;i++) {
allNames.push(names[i]['NAME']);
}
return allNames.sort();
But I can't figure out how to pass in the arrays into the function (and if possible I would like to just pass in the array name part instead of the whole json[0]['DATA']['array_name'] like I was trying to do in the first function...
Upvotes: 2
Views: 10968
Reputation: 130065
you can combine JSON easily with jQuery :
var x ={ a:1, b:2 };
var y ={ a:2, c:3 };
var z ={ b:3, d:4 };
$.extend(x, y, z);
console.dir(x); // now 'x' is all of them combined
Upvotes: 9
Reputation: 75307
If you've got 3 arrays like this:
[{ "id":1, "name":"Bob", "data":1},{ "id":2, "name":"Fred", "data":2 }]
Simply do:
function combine() {
var ar = [];
return ar.concat.apply(ar, arguments).sort(function (a, b) {
var aName = a.NAME;
var bName = b.NAME;
if (aName < bName) {
return -1;
} else if (aName == bName) {
return 0;
} else {
return 1;
};
});
};
Then call it like:
var jointArrays = combine(array1, array2, array3, ...);
However, if your JSON looks like this:
json[0]['DATA'][array_1]
json[0]['DATA'][array_2]
json[0]['DATA'][array_3]
You can simply define combine()
as follows, which will be more convenient:
function combine(arrays) {
var ar = [];
return ar.concat.apply(ar, arrays).sort(function (a, b) {
var aName = a.NAME;
var bName = b.NAME;
if (aName < bName) {
return -1;
} else if (aName == bName) {
return 0;
} else {
return 1;
};
});
};
Then call it like:
var jointArrays = combine(json[0].DATA);
If you're wanting an array of just the names, rather than the objects, use the following:
function combine(arrays) {
var ar = [],
ret = [];
ar = ar.concat.apply(ar, arrays);
for (var i=0;i<ar.length;i++) {
ret.push(ar.NAME);
};
return ret.sort();
};
Javascript is case sensitive; make sure it's DATA
and not data
, and NAME
and not name
.
Now for a little bit of housekeeping.
In your example, both of your counter variables are being declared as "implied globals", because you're not prefixing them with the var
statement (and implied globals are bad). You should use:
for (var i=0;i<something.length;i++) {
//
};
Instead of neglecting the var
.
Also, "{}" creates an object. "[]" creates an array. Javascript does not support associative array's; e.g array's with keys that are anything except a number. What you're JSON is returning is an array of objects
"Square notation" and "dot notation" are interchangeable. object["one"]
is equivalent to object.one
Square notation is generally used when the key is stored as a variable, or when you're accessing an array.
var key = "one";
object[key]
Hope this helps.
Upvotes: 4
Reputation: 2117
function allNames(names) {
var allNames = [];
for (var i=0;i<names.length;i++) {
for (var j=0;j<json[0]['DATA'][names[i]].length;j++) {
allNames.push(json[0]['DATA'][names[i]][j]['NAME']);
}
}
return allNames.sort();
}
called using:
allNames(['array_1','array_2']);
Seems to work.
Upvotes: 0
Reputation: 3790
You're redeclaring the allNames variable, emptying it.
Try this:
function combineNames(names) {
var allNames = [];
var data = json[0]['DATA'];
for (arrnames in data) {
for (j=0;j<data[arrnames].length;j++) {
if ('NAME' in data[arrnames]){
allNames.push(data[arrnames]['NAME']);
}
}
}
return allNames.sort();
}
Upvotes: 0