2
array["Hi","I","Hate","Love","You"];

how can I return "Hi I Love You" and delete the index "Hate".

Can I do this using Slice? From what I know if I use slice such as below:

array.slice(2,3);

It will return only "Hate" instead of getting rid of it, which is what I want.

5 Answers5

3

There is very similar function (language-wise) thats doing what you need

const array = ["Hi","I","Hate","Love","You"];
array.splice(2,1);
console.log(array);

With a little upgrade, you can ask your V8 about if someone likes you or not. Just say his/her name loud and click on Run code snippet. The first response is true, you cannot repeat it for the same person.

const array = ["Hi","I","Hate","Love","You"];
let i=0;
if (Math.random() > 0.5) {
  i++;
}
array.splice(2+i,1);
console.log(array);
libik
  • 19,204
  • 9
  • 35
  • 72
0

Try the splice method:

const a = [1, 2, 3, 4]
a.splice(1, 1); // idx, count
console.log(a);
Luan Nico
  • 4,792
  • 2
  • 27
  • 50
0

var arr = ["Hi","I","Hate","Love","You"];
var newarr = Array.prototype.concat(arr.slice(0,2), arr.slice(3));
console.log(newarr);
dgeare
  • 2,525
  • 5
  • 16
  • 27
0

_.remove from lodash library will do the job.

_.remove(array, function(item) {
  return item ===“Hate”;
})

https://lodash.com/docs/#remove

yeshashah
  • 1,428
  • 11
  • 19
0

You can use array filter method

var org = ["Hi", "I", "Hate", "Love", "You"];

let newArray = org.filter(function(item) {
  return item !== 'Hate'

});

console.log(newArray)
brk
  • 43,022
  • 4
  • 37
  • 61