-2

I am trying to merge two arrays data in one array format but getting empty array how to solve this please help me out this.

array1= ["hjghh", "hjghhs", "hjghhsjj"]
    0:
    0:"hjghh" 
    1:"hjghhs"
    2:"hjghhsjj"
    array2=["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"]
    0:
    0: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"
    1: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"
    2: "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"

in array3 I want to display like this

    0:
    0:["hjghh","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"]
    1:["hjghhs","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"]
    2:["hjghhsjj","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"]

this.barcodetextone is 1st array
this.ShowImgoriginal is 2nd array

let arr3:any=[];
this.barcodetextone.forEach((i) => {
    arr3.push(Object.assign({}, this.ShowImgoriginal[i]));
});

console.log(arr3);
2
  • 2
    Why use Object.assign? Just use arr3 = this.barcodetextone.map((elem, i) => [elem, this.ShowImgoriginal[i]]);. Commented Jul 22, 2019 at 11:12
  • Possible duplicate of Combine two arrays in JavaScript Commented Jul 22, 2019 at 12:40

2 Answers 2

0

Use the index in the first array to get the values for the second array and push an array with both:

let array1 = ["hjghh", "hjghhs", "hjghhsjj"];
let array2 = ["data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD"];
let result = [];

array1.forEach((i, index) => {
    result.push([i, array2[index]])
})

console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. You should accept the answer in case somebody has the same question in the future.
-1
    let arr1 = ['a','b','c']
    let arr2 = [1,3,51,51,2,1,0]
    let arr3 = [...arr1,...arr2]
    let zib = {}
    arr3.forEach((elem,index)=> {
        zib[index] = elem
    })

is this what you meant ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.