1

so I created an array with several objects inside it

const students = [
    {jean:14},
    {mike:19},
    {nean:16},
    {annie:17}
]

and I want to remove certain object from the array by using the object's index

let index = students.findIndex(i => {
    if (Object.keys(i) == 'nean'){
        return true
    }
})

and it returns the index of the object that I want to remove from the array and I do .splice() to remove it from the array. It does remove the object that I expect to be removed but it also removes the item after it

    students.splice(index, index)
    console.log(students) 

//(2) […] 0: Object { jean: 14 } 1: Object { mike: 19 } length: 2
6
  • Hi, In splice the second index should be the number of objects you want to remove from the array, so it should be always 1, but currently you are passing index. You should use students.splice(index, 1) Commented Apr 26, 2021 at 7:41
  • probably array.filter() is your friend Commented Apr 26, 2021 at 7:42
  • The logic of that if() doesn't really make sense. Neither does using unique keys in each object. Much simpler with structure like [{name:'jean', age:14},{name:'mike'...}] Commented Apr 26, 2021 at 7:42
  • what's the differences then between using unique keys in each object as i did and not Commented Apr 26, 2021 at 7:46
  • Just a lot simpler to code with consistent key names in each object Commented Apr 26, 2021 at 7:47

2 Answers 2

3

When using splice, the first argument is the index and the second is how many elements to remove from that index. so in order to remove just that element, you can do: students.splice(index, 1) reference: splice

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

Comments

-1

Array.splice() should be provided at least 2 arguments and from argument 3 to infinity are optional,

the first argument is the index where u want to start your manipulation, the second argument is the number of items that you would like to be deleted from that index onwards, and the third argument is the string/object that you would like to add

in your example you would like to start at the index that you have found using this function:


    let index = students.findIndex(i => {
        if (Object.keys(i) == 'nean'){
            return true
        }
    })

and you would like to delete just one! so you need to provide 1 for the second argument and not index,

js students.splice(index, 1)

as you don't need to add anything you will not provide the 3rd argument,

I hope this helps with all your array splice needs, good luck

8 Comments

Object.keys(i) == 'nean' will never be true
Array splice have only one required argument, start index.
@charlietfl why is that
@DylanMac Because Object.keys() returns an array developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@charlietfl thanks for all your comments that helps a lot!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.