5
\$\begingroup\$

I have an array and must fill another one.

Depending of a boolean I copy first or second element and the rest must be as is...

Currently I do it like this (data has the desired info):

const newArray = [];
newArray[0] = theBoolean ? data[0] : data[1];
for (let i = 2; i < data.length; i += 1) {
  newArray[i - 1] = data[i];
}

I've tried with shift and slice but no success...

¿how can I beautify this snippet?

\$\endgroup\$
4
  • \$\begingroup\$ Hey @Joc, I am posting this comment regarding this post stackoverflow.com/questions/42403946/… in Stackoverflow. Since I am in a 6 day suspension, I can't inreact with you there \$\endgroup\$
    – i--
    Mar 8, 2017 at 10:17
  • \$\begingroup\$ @SagarV tell me, you have some idea what's going on? \$\endgroup\$
    – joc
    Mar 8, 2017 at 12:31
  • \$\begingroup\$ tell me your exact requirement whether you want to add the page url or any other url with some text? \$\endgroup\$
    – i--
    Mar 8, 2017 at 12:39
  • \$\begingroup\$ @Sagar Yes, I want to share another ULR, concretely the actual URL+"#name" as stated in comments. Read question and comments carefully, all is there. \$\endgroup\$
    – joc
    Mar 8, 2017 at 12:40

3 Answers 3

5
\$\begingroup\$

You're on the right track trying slice. Here's an example of using it with no loops required.

Initialisation

You could initialise newArray with an element directly in the declaration, removing the need for newArray[0] = ...:

const newArray = [theBoolean ? data[0] : data[1]];

Pushing all other elements from data to newArray:

You could use Array.prototype.slice to get all other elements: data.slice(2).

Using spread operator:

const newArray = [theBoolean ? data[0] : data[1]];
newArray.push(...data.slice(2));

// or, one liner:
const newArray = [theBoolean ? data[0] : data[1], ...data.slice(2)];

Using Array.prototype.concat:

const newArray = [theBoolean ? data[0] : data[1]].concat(data.slice(2));

Using Function.prototype.apply:

const newArray = [theBoolean ? data[0] : data[1]];
[].push.apply(newArray, data.slice(2));
\$\endgroup\$
1
  • 1
    \$\begingroup\$ It should be noted that stack size is limited so you'll need to check if the number of elements for push.apply is less than some safe value e.g. 1024, otherwise use concat. \$\endgroup\$
    – wOxxOm
    Feb 28, 2017 at 15:27
2
\$\begingroup\$

First, I think your current technique could be improved by using push rather than array indices when inserting.

However another couple of options come to mind:

  1. Using Array.filter to remove the unwanted item

    const newArray = data.filter(function (_, index) {
        return theBoolean? index !== 1 : index !== 0;
    });
    
  2. To delete the unwanted item with splice if you do not mind mutating the source array.

array.splice(start, deleteCount)

start

Index at which to start changing the array (with origin 0). If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end of the array.

deleteCount Optional

An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.

data.splice(theBoolean? 1 : 0, 1);
\$\endgroup\$
1
\$\begingroup\$

How about:

const newArray = data.slice(1);
if (theBoolean) newArray[0] = data[0];
\$\endgroup\$
1
  • \$\begingroup\$ -1 for no explanation at all. \$\endgroup\$
    – t3chb0t
    Mar 1, 2017 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.