繁体   English   中英

如何使用 function 将 object 从一个阵列移动到另一个阵列?

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

大家好,亲爱的朋友们。 在学习 React 的过程中,我注意到我缺少 javascript,于是我开始认真学习 javascript。

让我试着告诉你我想要做什么。 我有两个 arrays。 我想将array1 中的实例元素的元素之一移动到array2 中的实例。

我该怎么做? 你会推荐哪种方法? 拼接元素并将其向下推是否有效? 如何为此编写 function? 如果你能帮忙,我会很高兴。

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 
        ],
      },
    ];

您可以使用pop()从数组中删除最后一个元素。

 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)

首先,您的 object 有错误的括号。 这是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' }
        ],
      },
    ];


现在删除一个元素,IDK 你是如何得到这个键的,所以我将它声明为常量,由你来修改

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

现在我们删除了元素,它存储在removed(长度=1的数组)中,现在插入它

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

从您的帖子来看,似乎您已经知道什么是拼接和推送以及它们的作用(名称非常清晰直观)。 如果我的回答中有任何您不理解的部分,请随时要求澄清

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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