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
Related
This question already has answers here:
using .join method to convert array to string without commas [duplicate]
(4 answers)
Convert array to string javascript by removing commas [duplicate]
(3 answers)
Closed 1 year ago.
I have an input field with one button. I store value of input in array.
My console.log output has below result
How can take the String "JOHN" from this array?
My code is:
var category = $('#customer_name_search').val();
var len = category.length;
var arr = [];
for(var i=0; i<len; i++){
var test = category[i];
arr.push(test);
}
console.log(arr);
You might want to use the join() method
var category = $('#customer_name_search').val();
var len = category.length;
var arr = [];
for(var i=0; i<len; i++){
var test = category[i];
arr.push(test);
}
console.log(arr.join(''));
here is some documentation
Use Array.Join().
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
// expected output: "Fire,Air,Water"
console.log(elements.join(''));
// expected output: "FireAirWater"
console.log(elements.join('-'));
// expected output: "Fire-Air-Water"
you can use array.join to merge the strings in an array
const array = ["J", "O", "H", "N"];
const merged = array.join("");
so the result will be "JOHN"
You just need to join the array by using array function(join).
Check out this link
var category = $('#customer_name_search').val();
var str = category.join('');
This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
Closed 1 year ago.
I'm using local storage to get an arrays of strings,
First value attrIds is as follows,
var attrIds = localStorage.getItem('attributeids');
attrIds = JSON.parse(attrIds);
Second value confOptions is as follows,
I want something like this,
144: "5595"
93: "5487"
I have tried creating a loop inside the loop and tried to set the key and value but it's not working. I have also tried to set the single JSON object as key and setting value as '' but couldn't move further with that.
Does anyone have any idea regarding this?
You can accomplish this using a simple for loop, accessing the items from the arrays, and assigning properties to an empty object.
const keys = ['144', '93'];
const values = ['5595', '5487'];
const obj = {};
for (let i = 0; i < keys.length; i++) {
obj[keys[i]] = values[i];
}
console.log(obj); // prints { 144: '5595', 93: '5487' }
Create a nested array and then use Object.fromEntries().
const
a = ["144", "93"],
b = ["5595", "5487"],
c = Object.fromEntries(a.map((v, i) => [v, b[i]]));
console.log(c);
Using a for loop, you could do something like:
var attrIds = localStorage.getItem('attributeids');
attrIds = JSON.parse(attrIds);
confOptions = ["5595", "5487"]
const object = {};
for(let i=0; i<attrIds.length;i++)
object[attrIds[i]] = confOptions[i]
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)
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);
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);