【问题标题】:Replacing an object in an array with places用位置替换数组中的对象
【发布时间】:2019-04-15 16:57:08
【问题描述】:

该函数将一个对象添加到数组中,并使其在列表中排在第二位。但在这种情况下,我紧紧抓住对象,我知道他创建后的编号是多少(5)。如何让它总是第二个添加?

const arr = [{
    "name": "BMW",
    "price": "55 000",
    "country": "Germany",
    "certificate": "yes"
  },
  {
    "name": "Mercedes-benz",
    "price": "63 000",
    "country": "Germany",
    "certificate": "yes"
  },
  {
    "name": "Mitsubishi",
    "price": "93 000",
    "constructor": "Bar John",
    "door": "3",
    "country": "Japan",
  },
  {
    "name": "TOYOTA",
    "price": "48 000",
    "max_people": "7",
    "country": "Japan",
    "certificate": "yes"
  },
  {
    "name": "Volkswagen",
    "price": "36 000",
    "constructor": "Pier Sun",
    "country": "Germany",
    "certificate": "no"
  },
];

function pushArr(arr, item) {
  arr.push(item);
  arr[5] = [arr[1], arr[1] = arr[5]][0];
}

let newArr = pushArr(arr, {
  "name": "Audi",
  "price": "89 000",
  "constructor": "Adolf Trump",
  "country": "Germany",
  "certificate": "yes"
});

console.log(arr);

【问题讨论】:

标签: javascript arrays methods


【解决方案1】:

const arr = [{
    "name": "BMW",
    "price": "55 000",
    "country": "Germany",
    "certificate": "yes"
  },
  {
    "name": "Mercedes-benz",
    "price": "63 000",
    "country": "Germany",
    "certificate": "yes"
  },
  {
    "name": "Mitsubishi",
    "price": "93 000",
    "constructor": "Bar John",
    "door": "3",
    "country": "Japan",
  },
  {
    "name": "TOYOTA",
    "price": "48 000",
    "max_people": "7",
    "country": "Japan",
    "certificate": "yes"
  },
  {
    "name": "Volkswagen",
    "price": "36 000",
    "constructor": "Pier Sun",
    "country": "Germany",
    "certificate": "no"
  },
];

function pushArr(arr, item) {
  let n = arr.length;
  arr.push(item);
  arr[n] = [arr[1], arr[1] = arr[n]][0];
}

let newArr = pushArr(arr, {
  "name": "Audi",
  "price": "89 000",
  "constructor": "Adolf Trump",
  "country": "Germany",
  "certificate": "yes"
});

let newArr2 = pushArr(arr, {
  "name": "Audi2",
  "price": "100 000",
  "constructor": "Adolf Trump",
  "country": "Germany",
  "certificate": "yes"
});

console.log(arr);

【讨论】:

    【解决方案2】:

    您可以使用destructuring assignment 获取变量中的第一个和剩余元素,然后将传入的值放在第二个位置返回新数组

    const arr = [{"name": "BMW","price": "55 000","country": "Germany","certificate": "yes"},{"name": "Mercedes-benz","price": "63 000","country": "Germany","certificate": "yes"},
    {"name": "Mitsubishi","price": "93 000","constructor": "Bar John","door": "3","country": "Japan" },
     {"name": "TOYOTA","price": "48 000","max_people": "7","country": "Japan","certificate": "yes"},
    {"name": "Volkswagen","price": "36 000","constructor": "Pier Sun","country": "Germany","certificate": "no"},];
    
    let atSecondAlways = (arr,newValue) => {
      let [first, ...remaining] = arr
      return [first,newValue,...remaining]
    }
    
    let newArr = atSecondAlways(arr,{ "name": "Audi", "price": "89 000",  "constructor": "Adolf Trump", "country": "Germany", "certificate": "yes"});
    
    console.log(newArr);

    【讨论】:

      【解决方案3】:

      你可以使用 Array.splice()。使用此方法,您可以向数组添加或删除项目。

      请小心,因为拼接会影响原始数组,因此在应用更改之前创建数组的副本很方便

      function pushArr(arr, item) {
        let newArr = [...arr]
        newArr.splice(1,0,item);
        return newArr;
      }
      

      【讨论】:

        【解决方案4】:

        您可以使用解构。顺便别忘了返回数组

        const arr = [{
            "name": "BMW",
            "price": "55 000",
            "country": "Germany",
            "certificate": "yes"
          },
          {
            "name": "Mercedes-benz",
            "price": "63 000",
            "country": "Germany",
            "certificate": "yes"
          },
          {
            "name": "Mitsubishi",
            "price": "93 000",
            "constructor": "Bar John",
            "door": "3",
            "country": "Japan",
          },
          {
            "name": "TOYOTA",
            "price": "48 000",
            "max_people": "7",
            "country": "Japan",
            "certificate": "yes"
          },
          {
            "name": "Volkswagen",
            "price": "36 000",
            "constructor": "Pier Sun",
            "country": "Germany",
            "certificate": "no"
          },
        ];
        
        function pushArr(arr, item) {
          const [first, ...rest] = arr;
          return [first, item, ...rest ];
        }
        
        let newArr = pushArr(arr, {
          "name": "Audi",
          "price": "89 000",
          "constructor": "Adolf Trump",
          "country": "Germany",
          "certificate": "yes"
        });
        
        console.log(newArr);

        【讨论】:

          猜你喜欢
          • 2016-10-01
          • 2018-10-03
          • 1970-01-01
          • 1970-01-01
          • 2020-01-16
          • 2020-11-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多