【问题标题】:How to use one array as keys for another array?如何使用一个数组作为另一个数组的键?
【发布时间】:2020-11-20 11:57:52
【问题描述】:

我有这两个数组,我想这样输出:

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

我该怎么做?可能很简单,但我不知道如何做到这一点,我也在stackoverflow上搜索过,但没有找到类似的东西..

这是一个 React 项目,我不知道这是否重要。 谢谢。

【问题讨论】:

    标签: javascript arrays reactjs


    【解决方案1】:

    看起来这些就是我们所说的并行数组:一个数组的索引n处的元素与索引n处的元素相关其他的。

    既然如此,您可以使用简单的for 循环和括号属性表示法:

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

    现场示例:

    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);

    您也可以使用mapObject.fromEntries 来创建对象,但它更复杂(虽然更短)并且涉及临时数组对象:

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

    现场示例:

    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);

    旁注:在您的输出中,您已将值 100200 等显示为字符串,但它们在您的输入中是数字。如果您希望它们成为字符串,只需随时转换它们,如下所示:

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

    现场示例:

    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);

    你会得到人们指向reduce,但reduce 仅在使用预定义的、可重用的reducer 函数进行函数式编程时才有用。否则,这只是一个过于复杂、容易出错的循环,使用实际循环会更清晰、更容易正确。


    在您提出的评论中:

    如果我想要它是这样的怎么办? [{ text: 'pt', value: 100, }, { text: 'es', value: 500, }]?

    为此,您需要为数组中的每个条目创建一个对象。您可以通过map 创建数组,也可以使用对象字面量 创建一个对象({text: "PT", value: 100} 和类似的,但从数组中获取值):

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

    或对text 属性使用速记属性表示法:

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

    现场示例:

    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);

    我将这些 text 值保留为大写,但如果您想在对象中将它们设为小写,请在它们上使用 .toLocaleLowerCase()

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

    【讨论】:

    • 如此简单,我无法弄清楚.. 非常感谢! :)
    • 如果我想要它是这样的怎么办? [{ text: 'pt', value: 100, }, { text: 'es', value: 500, }] ?抱歉浪费了你的时间,伙计,但我对这个真的很陌生..@T.J.克劳德
    • @HenriqueMota - 不用担心,我添加到答案的末尾。 :-)
    【解决方案2】:

    您可以使用.forEach 循环遍历一个数组,然后填充该对象。

    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);

    【讨论】:

    • 感谢您的时间好友!我正在使用@T.J. 的 for 技巧。克劳德说,再次感谢。
    【解决方案3】:

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

    【讨论】:

    • result 常量的用途是什么?注意forEach 总是返回undefined.
    【解决方案4】:
    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}
    

    【讨论】:

    • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更高,更有可能吸引投票。
    【解决方案5】:

    你可以在一个非常简单的循环中做到这一点:

    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);
    

    这将产生一个像这样的对象:

    { 
        key: 'val', 
        key2: 'val2', 
        key3: 'val3' 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多