简体   繁体   中英

React: Add values from array in another array

I'm trying to do this kind of operation but I don't understand how to do.

I have two array:

array1 = [elem1, elem2, elem3]

and a second array:

array2 = [elemA, elemB, elemC]

What i would to obtain is:

arrayFinal = [elem1:elemA, elem2:elemB, elem3:elemC]

How can I do??

Thank you so much.

EDIT:

the arrayFinal is not key:value, but the elem1:elemA is only the first value, elem2:elemB is the second value, for example using a join.

EDIT2:

I make an example of valure of array:

array1 = [0, 20, 40, 60, 80]
array2 = [-97:61:-1008; -97:60:-1008; -97:73:-1006, -98:70:-1008]

arrayFinal = [0:-96:61:-1009, 20:-97:61:-1008, 40:-97:60:-1008, 60:-97:73:-1006, 80:-98:70:-1008]

I See that there is a lot of confusion going on in the answers/comments, some people think you want to combine the arrays into key/value pairs, while others think you want to concat the arrays, while others think you want to make it an array where array1 is the index and array2 is the value at that index.

This is all caused by your formatting of [Elem1:ElemA, Elem2:ElemB]. which isnt even valid JS as far as i am aware.

However you edited your question, and now it seems like you want to combine them like [[Elem1, ElemA], [Elem2, ElemB]] in which case you can do that really easily in this way:

const array1 = ["elem1", "elem2", "elem3"];
const array2 = [1, 2, 3];

const result = array1.map((key, i) => [key, array2[i]]);

this will yield the result: [ [ 'elem1', 1 ], [ 'elem2', 2 ], [ 'elem3', 3 ] ]

You can do it like this:

const array1 = [0, 20, 40, 60, 80];
const array2 = ["-97:61:-1008;", "-97:60:-1008;", "-97:73:-1006", "-98:70:-1008"];
const arrayFinal = array1.map((item,index) => `${item}:${array2[index]}`);

If you are sure both arrays have the same length you can loop through the first one with a map and do something like that:

const arrayFinal = array1.map((item, i) => ({[item]: array2[i]}));

You'll get:

arrayFinal = [{elem1: elemA}, {elem2: elemB}...]

 const array1 = ['item1', 'item2', 'item3']; const array2 = [1, 2, 3]; const arrayFinal = array1.map((item, i) => ({[item]: array2[i]})); console.log(arrayFinal);

Your case is very close to the following utility functions:

https://lodash.com/docs/4.17.15#zip https://ramdajs.com/docs/#zip

Also, it could be implemented as follows:

function zip<A, B>(listA: A[], listB: B[]): Array<[A, B]> {
  return listA.map((a, i) => [a, listB[i]]);
}

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABALxgBwDwEEA0iBCAfABQA2MAzlFgFyJYDaAunuVfnfswJR1YBO-AIYBPDA1wEmhRAG8AUIkT8AplBD8kbagDoAtkLTFiQvDG6IAvDIanE2rjCZNuAbnkBfefIgIKcUhUdUjgAc2JUIwYARhwAJhwAZhZEBgAWHABWHAA2FzcgA

If you want a dictionary:

function zipObject<A extends (string | number | symbol), B>(listA: A[], listB: B[]): { [key in A]: B } {
  return Object.assign(
    {},
    ...listA.map((a, i) => ({ [a]: listB[i] })),
  );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM