how to create array object using integer variable javascript [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I need to fill an array object as like below using integer variable.
for(i=1; i<=2; i++)
arr[i].push({i:(100 * i)})
Expected result is:
arr = [{ 1:100,2:200},{1:100,2:200}]
Problem is, array created as like below
arr = [{i:100,i:200},{i:100,i:200}]

You need to push to arr instead of arr[i].
Also, you can't use a variable as a key in json directly.
var arr = [];
for(i=1; i<=2; i++)
{
var b = {};
b[i] = 100*i;
arr.push({[i]:(i*100)});
}
console.log(arr);

Related

Javascript push and loop [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 2 years ago.
I currently have this array: var arr = []
How do I push multiple "hello" strings into the array using a for loop?
I tried
var newArray = arr.push("hello")10;
try new Array forEach or simple for-loop should work.
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion #mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)

JavaScript matrix wired behaviour when using new Array() [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
When i use Array.fill to fill a multidimensional array, i get a weird behaviour when pushing to one of the arrays:
var arr = Array(2).fill([]);
arr[0].push(5);
console.log(arr);
//=> prints [[5], [5]]
fill is essentially doing this:
var content = [];
for (var i = 0; i < 2; i += 1) {
arr[i] = content;
}
So, your array will have a reference to the array you've passed to fill in each property.
It sounds weird, but what your code actually does is create an array ([]) and put a reference for that array in each of the items of the Array(2). So whenever you change that reference - every array that is referenced to that Array is changed.
It's exactly the same as:
var a = [];
var arr = Array(2).fill(a);
a.push(5);
console.log(arr[0][0], arr[1][0]);
a[0] = 2;
console.log(arr[0][0], arr[1][0]);
You can see that the values inside the arr are affected by the change to the a array.

Weird behaviour in Array.fill [duplicate]

This question already has answers here:
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 4 years ago.
When i use Array.fill to fill a multidimensional array, i get a weird behaviour when pushing to one of the arrays:
var arr = Array(2).fill([]);
arr[0].push(5);
console.log(arr);
//=> prints [[5], [5]]
fill is essentially doing this:
var content = [];
for (var i = 0; i < 2; i += 1) {
arr[i] = content;
}
So, your array will have a reference to the array you've passed to fill in each property.
It sounds weird, but what your code actually does is create an array ([]) and put a reference for that array in each of the items of the Array(2). So whenever you change that reference - every array that is referenced to that Array is changed.
It's exactly the same as:
var a = [];
var arr = Array(2).fill(a);
a.push(5);
console.log(arr[0][0], arr[1][0]);
a[0] = 2;
console.log(arr[0][0], arr[1][0]);
You can see that the values inside the arr are affected by the change to the a array.

make array multidimensional values and key pairs in javascript [duplicate]

This question already has answers here:
Combining two arrays to form a javascript object
(11 answers)
Closed 7 years ago.
How i get below output in javascript
var keys = ["1","2","3"];
var values = ["one", "two","three"];
var final = {
"1": "one",
"2": "two",
"3": "three"
}
Loop over one of the arrays and use the index to access the other, save in an object
var result = {};
keys.forEach(function(item, index) {
result[item] = values[index];
});
A very simple loop should suffice, copying the values & keys at each index into a new object:
var finalOutput = {};
for(var i=0, j=keys.length; i<j; i++) {
finalOutput[keys[i]] = values[i];
}
Working Example
Note: You shouldn't use final as a variable name as it is a reserved future keyword

javascript convert array to json object [duplicate]

This question already has answers here:
JavaScript: Converting Array to Object
(2 answers)
Closed 8 years ago.
I've got the following array:
array = [{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}]
i wanto convert it to json like this:
json = {{"id":144,"price":12500000},{"id":145,"price":13500000},
{"id":146,"price":13450000},{"id":147,"price":11500000},
{"id":148,"price":15560000}}
So than i can store everything in mongodb in a unique document.
Regards,
Just run a loop and equate...like...
var obj = {};
for(var i=0; i<array.length; i++)
{
obj[i] = array[i]
}
It will do
{
0:{"id":144,"price":12500000},
1:{"id":145,"price":13500000},
2:{"id":146,"price":13450000},
3:{"id":147,"price":11500000},
4:{"id":148,"price":15560000}
}
Because your JSON is invalid.
Not sure what you are asking
From array or another variable to json string =>
var str = JSON.stringify(thing);
From a json string to variable
var thing = JSON.parse(str);

Categories

Resources