0

I have an array like this:

  var CutRoadArray = [
            ['Location', '_Location'],
            ['Applicant Info', '_ApplicantInfo'],
            ['Details', '_ApplicationDetails'],
            ['Bond Info', '_BondInfo'],
            ['Attachments', '_Attachments'],
            ['Review', '_ReviewA']
        ];

I am trying to replace the last item, with a different entry.

The code I have so far goes like below:

                var newreviewElem = ['Review', '_ReviewB'];
                var index = CutRoadArray.lastIndexOf('_ReviewA');
                CutRoadArray.splice(index, 0, newreviewElem);
                console.log(CutRoadArray);

This is however not working. What am I doing wrong ?

https://jsfiddle.net/yygLdo0o/

5 Answers 5

1

There are actually two things wrong with your answer.

The first is that you need to grab the correct index. If you're replacing the last item, just grab the array.length.

The second is that you need to indicate how many you're replacing:

CutRoadArray.splice(CutRoadArray.length - 1, 1, newreviewElem);

The second argument in splice should be 1, not 0.

This will replace the last element of any size array, because it doesn't rely on an item in the array being in a specific location or a particular index.

CutRoadArray.length - 1 is grabbing the number of items in the array, but since splice uses a zero based index, you have to subtract one to get the index of the last item in the array.

The second argument (bolded below), tells splice to replace a single item.

Documentation about splice

CutRoadArray.splice(CutRoadArray.length - 1, 1, newreviewElem);

And then finally, the last argument is the item to actually add to the array.

Working fiddle

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

2 Comments

Thank you and yes, this works, can you please explain the CutRoadArray.splice(CutRoadArray.length - 1, 1, newreviewElem); part so me or anyone else who stumbles across this, would learn ?
thank you for the array 101, I hope this helps others as well.
1

It should be:

CutRoadArray.splice(index, 1, newreviewElem);

The second parameter indicates how many items should be replaced.

1 Comment

Thank you for your swift help.
1

Your

CutRoadArray.lastIndexOf('_ReviewA');

will, of course, not find anything since CutRoadArray contains arrays, not strings.

for(var iter = 0; iter < CutRoadArray.length; iter++) {
  if (CutRoadArray[iter][1] == '_ReviewA') {
    CutRoadArray[iter] = newreviewElem;
    break;
  }
}

Comments

1

If you want to replace the element, use

CutRoadArray.splice(index, 1, newreviewElem);

The second parameter of splice is the deleteCount, 0 means no item will be removed.

An other problem with your code is that

CutRoadArray.lastIndexOf('_ReviewA');

will always return -1, as CutRoadArray is an array of arrays, meaning every element of it is an array, it doesn't have an element which is '_ReviewA'. (That's an element of one of CutRoadArray's elements.)

3 Comments

What would be the right way to do this. Lets say I have a button or a checkbox, depending on its state, it would alter the element to reviewA or reviewB ?
@user3067743 it's hard to tell what's the right way to do this without knowing more of your use-case. You will probably have to look through all the elements of the array to find one containing your string.
the part in brackets, (That's an element of one of CutRoadArray's elements.) finally sunk in, your answer was swift and correct, I am accepting Blunderfest's answer only because it has a fiddle, someone wandering into this page could easily try the fiddle. Thank you ...
1

I suggest to iterate over the main array and search in the nested array for the wanted index. After found it is simple to replace the nested array at the index like array[index] = replace;. If not found, the the array is pushed to the end.

function replace(array, find, replace) {
    var index;
    if (array.some(function (a, i) {
            if (~a.indexOf(find)) {
                index = i;
                return true;
            }
        })
    ) {
        array[index] = replace;
    } else {
        array.push(replace);
    }
}

var cutRoadArray = [
      ['Location', '_Location'],
      ['Applicant Info', '_ApplicantInfo'],
      ['Details', '_ApplicationDetails'],
      ['Bond Info', '_BondInfo'],
      ['Attachments', '_Attachments'],
      ['Review', '_ReviewA']
];

replace(cutRoadArray, '_ReviewA', ['Review', '_ReviewB']);
document.write('<pre>' + JSON.stringify(cutRoadArray, 0, 4) + '</pre>');

Comments

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.