javascript - Reactjs trying to merge arrays - Stack Overflow

Let's say I have two array'slet array1 = ["H","E","", "&qu

Let's say I have two array's

let array1 = ["H","E","", "","O","","","R","L","D"];
let array2 = ["","","L","L","","W","O","","",""];

I want to merge them such that they would then contain:

array3 = ["H","E","L", "L","O","W","O","R","L","D"];

How would I achieve this? To be more clear I have a target array which is array3 an empty array and then I'm generating random characters and if they match array3 adding them to the blank array in the specific position with react state. It is just not storing the position and character each time but just changing it. SO my idea is to set the state such that I merge the current state with the new characters that are found.

TLDR:- Brute forcing Hello World meme.

Let's say I have two array's

let array1 = ["H","E","", "","O","","","R","L","D"];
let array2 = ["","","L","L","","W","O","","",""];

I want to merge them such that they would then contain:

array3 = ["H","E","L", "L","O","W","O","R","L","D"];

How would I achieve this? To be more clear I have a target array which is array3 an empty array and then I'm generating random characters and if they match array3 adding them to the blank array in the specific position with react state. It is just not storing the position and character each time but just changing it. SO my idea is to set the state such that I merge the current state with the new characters that are found.

TLDR:- Brute forcing Hello World meme.

Share Improve this question edited May 14, 2018 at 14:54 Chris 59.6k20 gold badges120 silver badges142 bronze badges asked May 14, 2018 at 14:29 user9069254user9069254 1632 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You can use Array.prototype.map() to create a new array array3 out of iterating over array1 and get the l (letters) and if any l evaluates to falsey then get the letter at the same i (index) in the array2.

Note that instead of declaring your arrays with let you should always use const because it makes code easier to read within its scope, and const variable always refers to the same object.

Code example:

const array1 = ["H","E","", "","O","","","R","L","D"];
const array2 = ["","","L","L","","W","O","","",""];

const array3 = array1.map((l, i) => l || array2[i]);

console.log(array3);

Try it:

let arr1 = ["H","E","", "","O","","","R","L","D"];
let arr2 = ["","","L","L","","W","O","","",""];
let arr3 = [];

arr1.forEach((val, index) => {
  if (val === '') {
    arr3[index] = arr2[index];
  } else {
    arr3[index] = val;
  }
});
console.log(arr3);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745577156a4634034.html

相关推荐

  • javascript - Reactjs trying to merge arrays - Stack Overflow

    Let's say I have two array'slet array1 = ["H","E","", "&qu

    12小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信