简体   繁体   English

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

[英]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.在学习 React 的过程中,我注意到我缺少 javascript,于是我开始认真学习 javascript。

Let me try to tell you what I am trying to do.让我试着告诉你我想要做什么。 I have two arrays.我有两个 arrays。 I want to move one of the elements of the instance element in array1 to the instance in array2.我想将array1 中的实例元素的元素之一移动到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?如何为此编写 function? 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.您可以使用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)

First thing, your object has wrong brackets.首先,您的 object 有错误的括号。 Here is a fixed version for array1这是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现在删除一个元素,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

Now we removed the element, and it is stored in removed (an array of length=1), just insert it now现在我们删除了元素,它存储在removed(长度=1的数组)中,现在插入它

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如果我的回答中有任何您不理解的部分,请随时要求澄清

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

相关问题 将对象从一个数组移动到另一个数组 - Move object from one array to another 如何将数组从一个 function 传递到 class 中的另一个? - How can I pass an array from one function to another in a class? 如何使用 hash 将元素从一个数组伪随机移动到另一个数组? - How can I move elements from one array to another pseudo-randomly using a hash? 如何按功能将 object 从一个阵列移动到另一个阵列? - How do you move an object from one array to another array by feature? 将 object 值从一个对象数组移动到另一个对象数组 - Move object value from one array of objects to another 在 React 中将映射对象从一个数组移动到另一个数组 - Move mapped object from one array to another in React 如何将数组的值从一个数组移动到另一个 [Javascript] - How do I move values ​of an array from one array to another [Javascript] 如何在javascript中将元素从数组移动到另一个数组? - How do I move an element from an array to another array in javascript? 如果条件(JavaScript ES6),将一个特定的 object 道具从一个数组移动到另一个对象数组 - move one specific object prop from one array to another array of objects, if condition (JavaScript ES6) 如何从另一个对象数组中按字面值减去一个对象数组 - How to subtract one object array literally from another object Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM