最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use one array as keys for another array? - Stack Overflow

programmeradmin3浏览0评论

I have these two arrays and I want to output it like this:

{
 "PT": "100",
 "ES": "400",
 "FR": "550",
 "CH": "200",
 "BR": "400",
 "DE": "500",
}

How can I do that? Probably its simple but I can't figure out how to do this, also I searched on stackoverflow and I didn't find anything like this..

This is a project in React, I don't know if that matter. Thanks.

I have these two arrays and I want to output it like this:

{
 "PT": "100",
 "ES": "400",
 "FR": "550",
 "CH": "200",
 "BR": "400",
 "DE": "500",
}

How can I do that? Probably its simple but I can't figure out how to do this, also I searched on stackoverflow and I didn't find anything like this..

This is a project in React, I don't know if that matter. Thanks.

Share Improve this question asked Nov 20, 2020 at 11:57 Henrique MotaHenrique Mota 1442 silver badges11 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 9

It looks like those are what we call parallel arrays: The element at index n of one array relates to the element at index n of the other.

That being the case, you can use a simple for loop and brackets property notation:

const result = {};
for (let index = 0; index < array1.length; ++index) {
    result[array1[index]] = array2[index];
}

Live Example:

const array1 = [
    "PT",
    "ES",
    "FR",
    "CH",
    "BR",
    "DE",
];
const array2 = [
    100,
    400,
    550,
    200,
    400,
    500,
];
const result = {};
for (let index = 0; index < array1.length; ++index) {
    result[array1[index]] = array2[index];
}
console.log(result);

You can also use map with Object.fromEntries to create the object, but it's more plicated (though shorter) and involves temporary array objects:

const result = Object.fromEntries(
    array1.map((array1value, index) => [array1value, array2[index]])
);

Live Example:

const array1 = [
    "PT",
    "ES",
    "FR",
    "CH",
    "BR",
    "DE",
];
const array2 = [
    100,
    400,
    550,
    200,
    400,
    500,
];
const result = Object.fromEntries(
    array1.map((array1value, index) => [array1value, array2[index]])
);
console.log(result);


Side note: In your output, you've shown the values 100, 200, etc. as strings, but they're numbers in your input. If you want them to be strings, just convert them as you go, like this:

const result = {};
for (let index = 0; index < array1.length; ++index) {
    result[array1[index]] = String(array2[index]);
// −−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^−−−−−−−−−−−−−^
}

Live Example:

const array1 = [
    "PT",
    "ES",
    "FR",
    "CH",
    "BR",
    "DE",
];
const array2 = [
    100,
    400,
    550,
    200,
    400,
    500,
];
const result = {};
for (let index = 0; index < array1.length; ++index) {
    result[array1[index]] = String(array2[index]);
}
console.log(result);


You'll get people pointing you at reduce, but reduce is only useful when you're doing functional programming with predefined, reusable reducer functions. Otherwise, it's just an overplicated, error-prone loop where using an actual loop would be clearer and easier to get right.


In a ment you've asked:

What If I want it do be like this? [{ text: 'pt', value: 100, }, { text: 'es', value: 500, }] ?

To do that, you want to create an object for each entry in the array. You can create the array via map, and you can create an object using an object literal ({text: "PT", value: 100} and similar, but getting the values from the array):

const result = array1.map((text, index) => ({text: text, value: array2[index]}));

or using shorthand property notation for the text property:

const result = array1.map((text, index) => ({text, value: array2[index]}));

Live Example:

const array1 = [
    "PT",
    "ES",
    "FR",
    "CH",
    "BR",
    "DE",
];
const array2 = [
    100,
    400,
    550,
    200,
    400,
    500,
];
const result = array1.map((text, index) => ({text, value: array2[index]}));
console.log(result);

I've left those text values in upper case, but if you want to make them lower case in the objects, use .toLocaleLowerCase() on them:

const result = array1.map((text: text.toLocaleLowerCase(), index) => ({text, value: array2[index]}));

const arr1 = [100, 200, 300]
const arr2 = ["PT", "AT", "CT"]
const obj = {}
arr1.forEach((item, index) => {
  obj[arr2[index]] = item
})


console.log(obj)

You can use .forEach to loop through one array and then populate the object.

const keys = ["PT", "ES", "FR", "CH", "BR", "DE"]
const values = ["100","400", "550", "200", "400", "500"]

const myObj = {}

keys.forEach((key,i) => myObj[key] = values[i]);

console.log(myObj);

a = ['a', 'b', 'c']
b= [1, 2, 3]
c = a.reduce((acc, item, index) => {
  acc[item] = b[index];
  return acc
}, {})
// {a: 1, b: 2, c: 3}

You can do this in a very simple loop:

const arr1 = ['key', 'key2', 'key3'];
const arr2 = ['val', 'val2', 'val3'];
const obj = {};

for (let i = 0; i < arr1.length; i++) {
    obj[arr1[i]] = arr2[i]; 
}

console.log(obj);

This will result in an object like this:

{ 
    key: 'val', 
    key2: 'val2', 
    key3: 'val3' 
}
发布评论

评论列表(0)

  1. 暂无评论