0

I've try to merge two array but it become like this image..

enter image description here

I want it when merged it become like this exaple

{0: {…}, storedArr: Array(2)}
0:
address: "ifanio de los Santos Ave, Mandaluyong, 1550 Metro Manila, Philippines"
addressNick: "test1"
latitude: 14.5724682
longitude: 121.0470239
remarks: "No.12"

1:
address: "ifanio de los Santos Ave, Mandaluyong, 1550 Metro Manila, Philippines"
latitude: 14.5724682
longitude: 121.0470239
remarks: "No.12"
__proto__: Object
length: 2
__proto__: Array(2)
__proto__: Obj

This is my example code as your reference

this.storage.get('storedAdd').then((addrr)=>{
      let storedArr = [];
      console.log(val)
        storedArr.push({
          address: val.address[0],
          addressNick: val.addressNickName,
          remarks: val.remarks,
          latitude: val.latitude,
         longitude: val.longitude
        });
        console.log(storedArr)
        console.log(addrr)
       this.storage.set('storedAdd', {...storedArr,...addrr});
      console.log({storedArr,...addrr})
      const datatransf = {...storedArr,...addrr}
      console.log(datatransf)
    this.packageAdd$.next(datatransf);
           
    })

2 Answers 2

3

You are trying to spread an array into an object here. This will give you an array inside the object under 0 index.

this.storage.set('storedAdd', {...storedArr,...addrr});

Are you sure this is what you wanted? Maybe it should be like this:

this.storage.set('storedAdd', [...storedArr,...addrr]);
Sign up to request clarification or add additional context in comments.

Comments

2

You are spreading your arrays into object { ...arr1, ...arr2 } instead of array [ ...arr1, ...arr2 ]

var arr1 = ['a', 'b', 'c']
var arr2 = ['d', 'e']

var merged = [...arr1, ...arr2]

console.log(merged)

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.