1

The function adds an object to the array and makes it second in the list. But in this case, I clung to the object and I know what his number after creation (5). How to make it so that it is always added second?

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);
Eddie
  • 26,593
  • 6
  • 36
  • 58
Gregori Roberts
  • 309
  • 3
  • 10
  • Please check [this post](https://stackoverflow.com/questions/586182/how-to-insert-an-item-into-an-array-at-a-specific-index-javascript/586189#586189) – Recep Karadas Apr 15 '19 at 17:05

4 Answers4

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);
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34
1

You can use destructuring assignment to take first and remaining elements in variable and than place the incoming value at second place return new array

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);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

You could use Array.splice(). With this method you can add or remove items to an array.

Just be careful because splice affects the original array so it's convenient to create a copy of the array before applying the change

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

You can use destructuring. don't forget to return the array by the way

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);
Maxime Girou
  • 1,511
  • 2
  • 16
  • 32