3
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.

0

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try the splice method:

const a = [1, 2, 3, 4]
a.splice(1, 1); // idx, count
console.log(a);

Comments

0

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

1 Comment

Great solution if you dont want it to be changed in place by splice
0

_.remove from lodash library will do the job.

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

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

Comments

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)

Comments