Convert arrays to son like string of objects in Nodejs - javascript

I have four arrays:
var attributes = ["a","b","c","d"];
var a = [1,2,3];
var b = [34,55,66];
var c = [22,23,53];
var d = [15,78,98];
How to merge them into one json like string in Nodejs?
[{"a":1,"b":34,"c":22,"d":14},
{"a":2,"b":55,"c":23,"d":78},
{"a":3,"b":66,"c":53,"d":98}]
Here is my code, but anyone have a better solution? I do need preserve the quote.
var a = [1,2,3];
var b = [34,55,66];
var c = [22,23,53];
var d = [15,78,98];
var obj = "[";
for (var u = 0; u < a.length; u++) {
var l = "\"a\":"+a[u]+",";
var m = "\"b\":"+b[u]+",";
var q = "\"b\":"+c[u]+",";
var n = "\"d\":"+d[u]+"";
if(u == (a.length-1))
var k = "{" + l + m + q + n + "}";
else
var k = "{" + l + m + q + n + "},";
console.log(k);
obj = obj + k;
};
obj = obj + "]";
console.log(obj);

Assuming all the arrays are the same length, and hardcoding their names:
var obj = []
for (var u = 0; u < a.length; u++) {
obj.push({
'a': a[u],
'b': b[u],
'c': c[u],
'd': d[u]
});
};
obj = JSON.stringify(obj);
EDIT: Converted the obj into a json string, the question had been mistakenly edited to ask for an array.

This piece of code will do the trick:
var arrays = [a, b, c, d]; // add all the arrays you want
var num = a.length; // or hardcode to the length you want
var result = [];
for(var i = 0; i < num; i++) {
var element = {};
attributes.forEach(function(attr, index) {
element[attr] = arrays[index][i];
});
result.push(element);
}
var attributes = ["a","b","c","d"];
var a = [1,2,3];
var b = [34,55,66];
var c = [22,23,53];
var d = [15,78,98];
var arrays = [a, b, c, d];
var result = [];
var num = a.length;
for(var i = 0; i < num; i++) {
var element = {};
attributes.forEach(function(attr, index) {
element[attr] = arrays[index][i];
});
result.push(element);
}
document.write(JSON.stringify(result));

var attributes = ["a","b","c","d"];
var a = [1,2,3];
var b = [34,55,66];
var c = [22,23,53];
var d = [15,78,98];
var x = [];
for (var i=0;i<3;i++) {
var obj = {};
for (var j=0;j<attributes.length;j++){
obj[attributes[j]]=eval(attributes[j]+"["+i+"]");
}
x.push(obj);
}
console.log(x);

I know there are multiple answers on this question already, but none seemed to handle the key arrays as variables who's names are introduced in the attributes array. Since I assumed that was the point of the attribute array, my solution does just that using node's global scope. It also does not assume that all the key arrays will be the same length as that is not specified (even though the example they are).
var attributes = ["a","b","c","d"];
var a = [1,2,3];
var b = [34,55,66];
var c = [22,23,53];
var d = [15,78,98];
var arr = [];
while(true){
var obj = {};
for(var i=0;i<attributes.length;i++){
if(global[attributes[i]].length)obj[attributes[i]]=global[attributes[i]].shift();
}
if(Object.keys(obj).length)
arr.push(obj);
else
break;
}
In the following fiddle, I had to emulate node's global scope by manually creating a global array, but you get the point:
Fiddle: https://jsfiddle.net/trex005/vm1yeL5d/
Important note If the arrays are not actually in the global scope, but are in some other object, you can use that object in place of global

Related

How to create array for loop

I want to create a array like this:
[{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}]
How can I do this in Javascript?
I have tried this:
for(i = 0; i < 3; i++){
var b = i;
var c = i+1;
var d = i+2;
};
dataResult={"b":b,"c":c,"d":d};
alert(dataResult) //not working result [{'b':0,'c':1,'d':2},{'b':1,'c':2,'d':3},{'b':2,'c':3,'d':4}]
You are just overriding value of 'b','c','d' and at the end assigning that value to 'dataResult', so you are not getting expected result.
Try this.
dataResult = [];
for(i = 0; i < 3; i++){
dataResult.push({ 'b': i, 'c': i+1, 'd': i+2 });
};
console.log(dataResult);
You'll have to create the object inside the loop, and then push it to the array:
const arr = [];
for (let i = 0; i < 3; i++) {
var b = i;
var c = i + 1;
var d = i + 2;
arr.push({ b, c, d });
}
console.log(arr);
But it would be a bit more elegant to use Array.from here:
const arr = Array.from({ length: 3 }, (_, i) => {
const b = i;
const c = i + 1;
const d = i + 2;
return { b, c, d };
});
console.log(arr);
Create the object inside the loop and push it to an array
var arr = [];
for (var i = 0; i < 3; i++) {
let obj = {
b: i,
c: i + 1,
d: i + 2,
}
arr.push(obj)
};
console.log(arr)
var myArr = [];
for(var i = 0; i < 3; i++){
var data = i;
myArr.push({
b: data,
c: data + 1,
d: data + 2
})
}
console.log(myArr)
You were creating the object outside the loop. You need to create object inside the loop.
Try following
var arr = [];
for(let i = 0; i < 3; i++){
var b = i;
var c = b+1; // as b = i, you can change c = b + 1
var d = c+1; // as c = i + 1, you can change d = c + 1
arr.push({b,c,d});
};
console.log(arr);
You are setting the value of b, c, d after it loops so it puts the latest value of b, c, d in dataResult. Instead, you should initialize dataResult with an empty array and push values to the array after every step of the loop
var a,b,c;
var dataResult = [];
for(i = 0; i < 3; i++){
b = i;
c = i+1;
d = i+2;
dataResult.push({"b":b, "c":c, "d":d});
};
alert(dataResult);

Array of objects contains same object over and over again

I create multiple objects and push them to the array objArr:
var objArr = [];
var obj = {};
var height = [9,8,7,3,6,5,2,4];
for (var i = 0; i < 8; i++) {
debugger;
var mountainH = height[i];
obj.h = mountainH;
obj.index = i;
objArr.push(obj);
}
for (var i = 0; i < objArr.length; i++) {
alert(objArr[i].h);
}
But as you can see, each object has the same values. Why?
Put the initialization of obj within your for-loop.
You were re-assigning new values to a global variable obj.
var objArr = [];
var height = [9,8,7,3,6,5,2,4];
for (var i = 0; i < 8; i++) {
debugger;
var obj = {};
var mountainH = height[i];
obj.h = mountainH;
obj.index = i;
objArr.push(obj);
}
for (var i = 0; i < objArr.length; i++) {
console.log(objArr[i].h);
}
Because the scope of obj in your code is global and it should rather be contained in the for loop.
If you will not declare it inside the loop then the value will get overwritten of the same obj on each iteration instead of a new memory allocation.
var objArr = [];
var height = [9, 8, 7, 3, 6, 5, 2, 4];
for (var i = 0; i < 8; i++) {
debugger;
var mountainH = height[i];
var obj = {};
obj.h = mountainH;
obj.index = i;
objArr.push(obj);
}
console.log(obj);
As noted, you need to initialize a new object in each iteration of the loop, otherwise all your array members simply share the same object reference.
Additionally, your code can be greatly reduced by building the array using .map(), and fully using the object literal initializer to declare the properties.
var height = [9,8,7,3,6,5,2,4];
var objArr = height.map((n, i) => ({h: n, index: i}));
console.log(objArr);
This is shorter and clearer. For every number in height, it creates a new object and adds it to a new array, which is returned from .map().
It can be even a little shorter with the newer features for object literals.
var height = [9,8,7,3,6,5,2,4];
var objArr = height.map((h, index) => ({h, index}));
console.log(objArr);

Javascript array becomes null

I defined two-dimensional array, tried to fill it in nested loop, but it fill only first dimension with right values, other dimensions are filled with null(or undefined), thanks.
var Arr = [];
var i =0;
for(i=0;i<13;i++)
{
Arr[i]=[];
}
var a=0,b=0;
for(a=0;a<13;a++)
{
for(b=0;b<13;b++)
{
Arr[[a][b]]=AnotherArrWithRightValues[(13 * a) + b];
}
}
Arr[[a][b]] should be Arr[a][b]
Loksly's answer is correct, but implemented in a different way. To answer your question, replace Arr[[a][b]] with Arr[a][b].
Full Code :
var Arr = [];
for(var a = 0 ; a < 13 ; a++) {
Arr[a] = [];
for(var b = 0 ; b < 13 ; b++) {
Arr[a][b]=AnotherArrWithRightValues[(13 * a) + b];
}
}
Just for the record, another way to achieve the same:
var Arr = [];
var i = 0, tmp;
var a, b;
for(a = 0; a < 13; a++){
tmp = [];
for(b = 0; b < 13; b++){
tmp.push(AnotherArrWithRightValues[i++]);
}
Arr.push(tmp);
}
Try this,
var arr =[];
for(var a=0;a<13;a++)
{
arr[a] = new Array();
for(var b=0;b<13;b++)
{
arr[a].push((13 * a) + b);
}
}
i hope this will help you

How to merge elements in array by first words in JS? Is it possible?

I have the following array
['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod']
I want to get this array from it:
['.some_class &.green_mod &.red_mod','another_class &.green_mod &.orange_mod']
Is it possible?
you can try:
var preArr = ['.some_class &.green_mod', '.some_class &.red_mod', 'another_class &.green_mod', 'another_class &.orange_mod'];
var newArr = [];
preArr.forEach(function (item) {
var has = false;
var preWords = item.split('&');
for (var i = 0; i < newArr.length; ++i) {
var newWords = newArr[i].split('&');
if (newWords[0] == preWords[0]) {
has = true;
for (var j = 0; j < preWords.length; ++j) {
if (newWords.indexOf(preWords[j]) < 0) {
newWords.push(preWords[j]);
}
}
newArr[i] = newWords.join('&');
}
}
if (!has) {
newArr.push(item);
}
});
console.log(newArr);
demo
var test = (function() {
var fmap1 = function(e) { return e.trim().split(/\s+/); };
var fmap2 = function(e) { return e.join(" "); };
var fsort = function(e1,e2) { return e1[0] == e2[0] ? 0 : e1[0] > e2[0] ? 1 : -1; };
return function test(a) {
var a1 = a.map(fmap1).sort(fsort);
var s, a2 = [];
for (var i = 0, l = a1.length; i < l; i++) {
if (s != a1[i][0]) {
s = a1[i][0];
a2.push([s]);
}
a1.push.apply(a2[a2.length - 1], a1[i].slice(1));
}
return a2.map(fmap2);
};
})();
var arr = [' .some_class &.green_mod',' another_class &.green_mod','.some_class &.red_mod','another_class &.orange_mod'];
console.log(test(arr));
This seems to work for me:
var arr = ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod'];
var obj = {};
var finalArr = [];
for(var i=0,c=arr.length;i<c;i++)
{
var parts = arr[i].split(' ');
var key = parts[0];
if(!obj[key]) obj[key] = [];
obj[key].push(parts.slice(1).join(' '));
}
var keys = Object.keys(obj);
for(var i=0,c=keys.length;i<c;i++)
{
var key = keys[i];
finalArr.push(key+' '+obj[key].join(' '))
}
console.log(finalArr);
Basically just loop through each one, and use the first word as a key to an array of strings to be appended, then loop through the object and join the keys with all of their array elements.
N.B. References to key are the first word
Solution bellow for data like key+separator+value where you are sure that there is no duplication in keys or You do not care if values are duplicated
// maping function that treats part before separator as key and saves incremetaly
// all values under that key (duplicated vales are possible)
// only one separator per input entry is allowed
function mapFun (el, obj, separator) {
var e = el.split(separator);
var key = e[0];
var val = e[1];
obj[key] = obj[key] ? obj[key] + separator + val : separator + val;
}
function combineClasses (arr) {
var result = [];
var separator = ' &';
var obj = Object.create(null); // create empty object without any properties or inheritance chain
for (var i=0; i<arr.length; i++) {
mapFun(arr[i], obj, separator);
}
for (var p in obj) {
result.push (p + obj[p]);
}
return result;
}
combineClasses(arr);
Bellow solution will work for data like before and data like key+separator+value1+separator+value2 and will not allow for duplicated values for the same key
function mapFun (el, separator, obj) {
var parts = el.split(separator);
var key = parts[0];
if (!obj[key]) {
obj[key] = Object.create(null);
}
for (var i=1; i<parts.length; i++) {
obj[key][parts[i]] = null;
}
}
function combineClasses (arr) {
var result = [];
var separator = ' &';
var obj = Object.create(null); // create empty object without any properties
for (var i=0; i<arr.length; i++) {
mapFun(arr[i], separator, obj);
}
for (var p in obj) {
var values = Object.keys(obj[p]).join(separator);
result.push (p + separator + values);
}
return result;
}
combineClasses(arr);
arr is where Your data comes in
var arr = ['.some_class &.green_mod','.some_class &.red_mod','another_class &.green_mod','another_class &.orange_mod']

Creating a dynamic Javascript object tree using an array?

Not sure if this can even be done, but I'll ask anyway:
Suppose if I have an array of names:
['bob', 'sue', 'dan']
And I want to dynamically create an object from those names:
bob.sue.dan = 5;
Is it possible?
Here you go, will preserve existing objects:
var namespace = function(name, separator, container){
var ns = name.split(separator || '.'),
o = container || window,
i,
len;
for(i = 0, len = ns.length; i < len; i++){
o = o[ns[i]] = o[ns[i]] || {};
}
return o;
};
e.g. usage:
namespace("com.example.namespace");
com.example.namespace.test = function(){
alert("In namespaced function.");
};
or for your example using an array.
var ns = ['bob', 'sue', 'dan'];
namespace(ns.join('.'));
bob.sue.dan.foobar = true;
or extending an existing object:
var bob = {}
namespace("foo.bar",".",bob);
bob.foo.bar = true;
Edit: updated as requested:
var namespace = function(name, separator, container, val){
var ns = name.split(separator || '.'),
o = container || window, i, len;
for(i = 0, len = ns.length; i < len; i++){
var v = (i==len-1 && val) ? val : {};
o = o[ns[i]] = o[ns[i]] || v;
}
return o;
};
namespace("bob.sue.dan",null,null,5);
alert(bob.sue.dan);
See working example: http://jsfiddle.net/herostwist/hu6j9/
Then you can do:
function makeOjectTree(propNames) {
var name;
var o = {};
var result = o;
for (var i=0, iLen=propNames.length; i<iLen; i++) {
name = propNames[i];
if (!o[name]) {
o[name] = {};
o = o[name];
}
}
return result;
}
var names = ['bob', 'sue', 'dan'];
var objs = [];
for(var i=0; i<names.length; i++) {
objs.push(names[i]);
var val = (i==names.length-1) ? "5" : "{}";
eval(objs.join(".") + " = " + val);
}
alert(bob.sue.dan);
Demo: http://jsfiddle.net/EpZm2/1/
sure you can ...
var obj = 5;
var yourarray = ['bob', 'sue', 'dan'];
yourarray = yourarray.reverse();
for(e in yourarray) {
var tmpobj = obj;
obj = new Object();
obj[yourarray[e]] = tmpobj;
// if you already have an object
if (e+1 == yourarray.length) {
your_current_existing_object[yourarray[e]] = tmpobj;
}
}
Yes this is possible.
You can define new properties on an object this way:
var obj = {};
obj["bob"] = {};
obj["bob"]["sue"] = {};
obj["bob"]["sue"]["dan"] = 5;
So you can also do it with an array of property names ;)

Categories

Resources