Concatenating JSON Arrays
I have 3 json arrays, each containing information specified in the same format:
Array:
ID:
NAME:
DATA:
ID:
NAME:
DATA:
etc...
My goal is to concatenate all 3 arrays into one array and sort and display the NAME by passing the 3 arrays to the function.
The function I tried:
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 is giving me the error that NAME is null or undefined.
I also tried using the array.concat function that works when I hardcode 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 arrays to the function (and if possible, I would like to just pass part of the array name instead of the whole json [0] ['DATA'] ['array_name'], as I tried to do in the first function. ..
a source to share
If you have 3 arrays:
[{ "id":1, "name":"Bob", "data":1},{ "id":2, "name":"Fred", "data":2 }]
Just 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 this:
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 is 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 this:
var jointArrays = combine(json[0].DATA);
If you only want an array of names, not 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 DATA
's not DATA
and NAME
not NAME
.
Now for a little household.
In your example, both of your counting variables are declared as "implied globals" because you are not prefixing them with a statement var
( and implied globals are bad ). You must use:
for (var i=0;i<something.length;i++) {
//
};
Instead of disdain var
.
In addition, "{}" creates an object. "[]" creates an array. Javascript does not support associative arrays; for example an array with keys that are anything but a number. What you are returning JSON is an array of objects
"Square notation" and "dot notation" are used interchangeably. object["one"]
equivalent toobject.one
Square notation is usually used when the key is stored as a variable or when you are accessing an array.
var key = "one";
object[key]
Hope it helps.
a source to share
You are updating the allNames variable by freeing it.
Try the following:
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();
}
a source to share