简体   繁体   中英

How can I move an object from one array to another array with function?

Hello everyone and dear friends. While learning React, I noticed that I lacked javascript and I started to learn javascript carefully.

Let me try to tell you what I am trying to do. I have two arrays. I want to move one of the elements of the instance element in array1 to the instance in array2.

How can I do it? Which method would you recommend? Does it work to splice the element and push it down? How can a function be written for this? I would be glad if you help.

const array1 = [
      {
        id: '1',
        instance: [
            { id: '34', title: 'Example 1' },
            { id: '35', title: 'Example 2' },
            { id: '36', title: 'Example 3' },  // delete this object from here 
      },
      {
        id: '2',
        instance: [
            { id: '37', title: 'Example 4' }
        ],
      },
    ];

I want to move the element I deleted into this array.

const array2 = [
      {
        id: '1',
        instance: [
            { id: '34', title: 'Example 1' },
            { id: '35', title: 'Example 2' },
      },
      {
        id: '2',
        instance: [
            { id: '37', title: 'Example 4' },
        //  { id: '36', title: 'Example 3' }, // i want to move here 
        ],
      },
    ];

You can use pop() to remove last element from array.

 const array1 = [{ id: '1', instance: [{ id: '34', title: 'Example 1' }, { id: '35', title: 'Example 2' }, { id: '36', title: 'Example 3' } ], }, { id: '2', instance: [{ id: '37', title: 'Example 4' }], }, ]; let tmp = array1[0].instance[2]; array1[0].instance.pop(); array1[1].instance.push(tmp); console.log(array1);

 const array1 = [ { id: '1', instance: [ { id: '34', title: 'Example 1' }, { id: '35', title: 'Example 2' }, { id: '36', title: 'Example 3' }, // delete this object from here ] }, { id: '2', instance: [ { id: '37', title: 'Example 4' } ] } ]; const array2 = [ { id: '1', instance: [ { id: '34', title: 'Example 1' }, { id: '35', title: 'Example 2' }, ] }, { id: '2', instance: [ { id: '37', title: 'Example 4' }, // { id: '36', title: 'Example 3' }, // i want to move here ] } ]; //answer function transfer(fromArr,index,toArr){ toArr.push(fromArr.splice(index,1)) } transfer(array1[0].instance,2,array2[1].instance) console.log("first array",array1) console.log("second array",array2)

 const array1 = [ { id: '1', instance: [ { id: '34', title: 'Example 1' }, { id: '35', title: 'Example 2' }, { id: '36', title: 'Example 3' },] // delete this object from here }, { id: '2', instance: [ { id: '37', title: 'Example 4' } ], }, ]; var temp = array1[0] array1.splice(0,1); const array2 = [ { id: '1', instance: [ { id: '34', title: 'Example 1' }, { id: '35', title: 'Example 2' },] }, { id: '2', instance: [ { id: '37', title: 'Example 4' }, // { id: '36', title: 'Example 3' }, // i want to move here ], }, ]; array2.push(temp) console.log(array2)

First thing, your object has wrong brackets. Here is a fixed version for array1


const array1 = [
      {
        id: '1',
        instance: [
            { id: '34', title: 'Example 1' },
            { id: '35', title: 'Example 2' },
            { id: '36', title: 'Example 3' },  // delete this object from here 
            ]
      },
      {
        id: '2',
        instance: [
            { id: '37', title: 'Example 4' }
        ],
      },
    ];


Now remove one element, IDK how you're getting the key for this, so I ill just declare it as constant, up to you to modify

let key = 0;
let Nested_key = 2;
let removed =  array1[key].instance.splice(Nested_key,1) //1st param= 0: the position, 2nd param = 1 number of elements to rm starting from param1 position

Now we removed the element, and it is stored in removed (an array of length=1), just insert it now

array2[key].instance.push(removed[0]);

Judging by your post, it Seems like you knew already what splice and push are and what they do (the name are quite clear and intuitive). Feel free to ask for clarifications if there is any part that you don't understand in my answer

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