用位置替换数组中的对象

时间:2019-04-15 16:57:08

标签: javascript arrays methods

该函数将一个对象添加到数组,并使其在列表中排第二。但是在这种情况下,我紧紧抓住对象,并且知道创建后他的编号是多少(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);

4 个答案:

答案 0 :(得分:2)

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);

答案 1 :(得分:1)

您可以使用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);

答案 2 :(得分:0)

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

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

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

答案 3 :(得分:0)

您可以使用解构。别忘了用

的方式返回数组

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);