How Push Array Using Javascript With This Case? [duplicate] - javascript

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
I have array like this:
var data = [[]];
var dataIndex = ['a','b','c'];
var data0 = [1,2,3];
var data1 = [4,5,6];
var data2 = [7,8,9];
I want to make that array to be like this:
data['a'] = [1,2,3];
data['b'] = [4,5,6];
data['c'] = [7,8,9];
I've tried with this code, but it doesn't work.
for(var i=0;i<dataIndex;i++){
data[data[i]].push('data'+i[i]);
}
I don't know to do that in javascript since I'm new on this. Anyone can help me? thank you.

By Using function and loop.
var data = {};
var dataIndex = ['a','b','c'];
var data0 = [1,2,3];
var data1 = [4,5,6];
var data2 = [7,8,9];
function covert(){
const allData = [data0,data1,data2]
dataIndex.forEach((item,i)=>{
data[item] = allData[i];
})
}
covert();
console.log(data)

Consider using an object(associative array) to save the values
Define your data object as follows
var data = {}
// You can now do the insertion operations as follows
data['a'] = [1,2,3]
// data in your object will be as follows
{'a': [1,2,3]}

You can just do it like this then. No need for loops
var data0 = [1,2,3];
var data1 = [4,5,6];
var data2 = [7,8,9];
var data = {
a : data0,
b : data1,
c : data2
};

Related

Pushing an Object to Array in js

I'm trying to push an Object to an Array using the following command:
a = a.concat(b);
a = [];
b = {a: Object1, b: Object2 ....};
So I wan to have array a to be like a = [Object1, Object2...]
But the above statement results in:
a = [[Object1, Object2...][Object11, Object12...]];
How can I achieve this in JS?
If I´m understanding what you want, you want just objects, not the keys. So, could be:
for( var key in b ){
var value = b[key];
a.push(value);
}
console.log(JSON.stringify(a));
Hope it helps...
You are concatenating two arrays with Array.concat() you need to use Array.push().
var a = [ObjectA];
var b = ObjectB;
a.push(b);
// a = [ObjectA, ObjectB];
let a = [];
let b = {a:1, b:2, c:3};
for(obj in b){
let val = {};
val[obj] = b[obj];
a.push(val);
}
console.log(a);
Did you mean something like this ?
The Object.values() method returns an array of a given object's own enumerable property values,
You could use the Object values method Object.values(), like :
Object.values(b);
Example :
var a = [];
var obj_1 = {'attr_1': 'val_1', 'attr_2': 'val_2'};
var obj_2 = {'attr_3': 'val_3', 'attr_4': 'val_4'};
var b = {'a': obj_1, 'b': obj_2};
a = Object.values(b);
console.log( a );
Hope this helps.

How to combine arrays in JavaScript? [duplicate]

This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Closed 8 years ago.
var arrayOne = [a,b,c];
var arrayTwo = [d,e,f];
How can I combine the two arrays in JavaScript so that the result will be:
var array = [a,d,b,e,c,f];
Note: This is not concatenating two arrays. This is merging the two arrays so the indices look like [0,0,1,1,2,2].
Assuming that the two arrays are equal of length, this should do the trick:
var arrayThree = [];
for(var i = 0; i < arrayOne.length; i++){
arrayThree.push(arrayOne[i], arrayTwo[i]);
}
If they aren't the same length and you would have the two arrays:
var a = [a,b,c,d];
var b = [e,f];
I assume you would want the the following result
var c = [a,e,b,f,c,d];
That should do the trick:
var c = [];
while(Math.min(a.length, b.length)){
c = c.concat(a.splice(0,1), b.splice(0,1));
//Or c = c.concat(b.splice(0,1), a.splice(0,1));
//depending on the order
}
a.length ? c = c.concat(a) : c = c.concat(b);
//Here is a = [c,d] and b = []
You can use jQuery's unique method along with concat
var a = [1,2,3];
var b = [2,3,4];
var c = a.concat(b); // [1,2,3,2,3,4]
c = $.unique(c); // [1, 2, 3, 4]
You may need to sort the array as needed.

How to insert array into another array using javascript

How to push array in another array using JavaScript?
Example:-
var a = [1,2,3,4,5]
var b = [6,7,8,9,10]
i want to push a & b into c
var c =[[1,2,3,4,5],[6,7,8,9,10]]
Basically what sam said is enough:
var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);
To convert to JSON:
var myJsonString = JSON.stringify(yourArray);
And to get it into an object :
var arrFromJson = JSON.parse( myJsonString );
Simple, you just have to create another array to do it:
var c = [a,b];
Then, you can use .push to add more arrays.
c.push([11,12,13]);
var a = [1,2,3,4,5];
var b = [6,7,8,9,10];
var c =[];
c.push(a);
c.push(b);

Stringify JavaScript object

I'm looking to stringify an object.
I want in output like this
{"1":{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, "2": {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}}
But I get this
{"valeur":"dalebrun","usager":"experttasp","date":"2013-08-20 16:41:50"}, {"valeur":"test","usager":"experttasp","date":"2013-08-20 16:41:50"}
What I do
var objVal = {}; //value....
var data = {}; //other value....
var object = $.extend({}, objVal, data); //concat the object
JSON.stringify(object);
When you concat the object, you get an array; you want a map with two elements, using the id "1" and "2"
var objVal = {}; //value....
var data = {}; //other value....
var object = {}
object["1"] = objVal;
object["2"] = date;
JSON.stringify(object);
I found the solution !
I do an for loop on the object. And I iterate on each element in the object. Thank you for your help. The answer of #Giovanni help me to found the solution.
Solution:
var data = {}; //values....
var objVal = {}; //other values....
var final = {};
var index = 1;
for(var key in data)
{
final[index] = data[key];
index = index + 1;
}
final[index] = objVal;
JSON.stringify(final);
And the output is :
{"1":{"valeur":"dfgdfg","usager":"experttasp","date":"2013-08-23 10:36:54"},"2":{"valeur":"uuuuuuuuuu","commentaire":"defg","usager":"experttasp","date":"2013-08-23 10:37:26"},"3":{"valeur":"uuuuuuuuuu","commentaire":"yesssss","usager":"experttasp","date":"2013-08-23 10:38:38"}}

creating a array in javascript

is this:
var arr = {};
the same as
var arr = new Array();
?
It is not the same.
var arr = {};
initializes an object. If you want an array:
var arr = [];
not exactly. var arr = []; is more like it.
No.
var arr = {}; // creates a new object with no properties
But
var arr = []; // creates a new blank array
var arr = {}; creates object
No, it's not: You're confusing array literals
var arr = []; // same as new Array()
with object literals
var obj = {}; // same as new Object()

Categories