【问题标题】:Move an element to the front of an array: Javascript [duplicate]将元素移动到数组的前面:Javascript [重复]
【发布时间】:2021-05-15 02:12:29
【问题描述】:

我有一个具有这种格式的对象数组

let arr = [ { name: "test1", id: 5}, { name: "test2", id: 6 } , { name: "test3", id: 8 } ]

现在我基本上想通过重新排列将带有 6 的项目移动到数组的前面,这样结果就变成了

let result = [ { name: "test2", id: 6 } , { name: "test1", id: 5}, { name: "test3", id: 8 } ]

我尝试过的

   const found = arr.find((element) => {
        return element.id === 6;
   });
   if (found) {
        const [...arr, found] = arr;
        return found;
      } else {
        return arr;
   }

【问题讨论】:

  • 使用.splice()删除该项目,然后使用.unshift()将其添加到前面。

标签: javascript arrays ecmascript-6 ecmascript-5


【解决方案1】:

您可以使用Array.unshift() 将元素添加到数组的开头,并使用Array.splice() 删除数组元素。

let arr = [ { name: "test1", id: 5}, { name: "test2", id: 6 } , { name: "test3", id: 8 } ]
let result = [...arr];

const index = result.findIndex(e => e.id === 6)
result.unshift(result.splice(index, 1)[0])

console.log(result);

【讨论】:

    【解决方案2】:

    您可以使用检查的增量对数组进行排序。

    const
        array = [{ name: "test1", id: 5 }, { name: "test2", id: 6 }, { name: "test3", id: 8 }];
    
    array.sort((a, b) => (b.id === 6) - (a.id === 6));
    
    console.log(array);

    【讨论】:

      【解决方案3】:

      您可以使用Array.unshiftArray.splice

      let arr = [{name:"test1",id:5},{name:"test2",id:6},{name:"test3",id:8}]
      
      const moveToFront = (data, matchingId) => {
        //find the index of the element in the array
        const index = data.findIndex(({id}) => id === matchingId);
        if(index !== -1) {
          //if the matching element is found, 
          const updatedData = [...data];
          //then remove that element and use `unshift`
          updatedData.unshift(...updatedData.splice(index, 1));
          return updatedData;
        }
        //if the matching element is not found, then return the same array
        return data;
      }
      
      console.log(moveToFront(arr, 6));
      .as-console-wrapper {
        max-height: 100% !important;
      }

      【讨论】:

        【解决方案4】:
        const array = [{ name: "test1", id: 5 }, { name: "test2", id: 6 }, { name: "test3", id: 8 }];
         const sortedArray = array.sort((a, b) => (b.id === 6) - (a.id === 6)); console.log(sortedArray);
        

        讨论了一种按 Array 对象中每个项目的索引号顺序对 JavaScript Array 进行排序的简单方法。如果您想按字母顺序排序并且不需要使用 String.sort() 之类的函数创建新的字符串数组,这会很有帮助。

        如果您想快速解决在 JavaScript 中对数组进行排序的方法,对您可能有用的快速提示如下。请记住,使用语言中内置的数组方法始终是最佳实践。它们的创建是为了快速高效地工作。但是,如果您真的想按索引号对数组进行排序并且没有字符串数组,那么本文将适合您。:

        String→Number:当函数返回一个 Number 值时,JavaScript 将其解释为等于代码中的 Number 值...该函数通过传递两个参数来使用,当它们相等时应该返回 true 并且当它们不相等时为假。在这种情况下,我们有点反向比较它们。我们正在检查 Array 中项目的 ID 以查看它们是否匹配,但我们减去 1 以检查它们是否小于。这是因为当我们调用 .sort() 时,JavaScript 会按字母顺序排序,并且值为 6 的 ID 将位于列表的末尾。因此,值 -1 将使其以正确的顺序出现。如果您想将此方法用于您的 Array,请在下面添加评论!

        【讨论】:

          【解决方案5】:

          您可以使用filterunshift

          let arr = [{ name: "test1", id: 5 },{ name: "test2", id: 6 },{ name: "test3", id: 8 }];
          let firstObject;
          let result = arr.filter((value) => {
            if (value.id != 6) return value;
            firstObject = value;
          });
          result.unshift(firstObject);
          console.log(result);

          【讨论】:

            猜你喜欢
            • 2020-05-12
            • 2016-10-10
            • 2017-05-13
            • 2021-08-12
            • 1970-01-01
            • 2018-03-21
            • 2014-08-07
            • 2019-10-05
            • 1970-01-01
            相关资源
            最近更新 更多