简体   繁体   中英

How do I refer position of an element in Array javascript. (I am not getting the expected result)

What I am trying to achieve is that whenever a new client is added to the clients array, I want the function to return "Welcome (new customer name), you are (his position in the array) in line."

What am I doing wrong? I am trying to get the index of the new client to add 1 so that it starts to count from 1.

    let clients = ['Smith', 'Stebve', 'John']

    function clientQue(array, newCustomer) {
        array.splice(1, 0, newCustomer)
        return "Welcome " + newCustomer + ", you are number " +  parseInt(array.indexOf('newCustomer')) + 1 + " in line.";
    }
    clientQue(clients, 'Bob');

You are missing paranthesis () to break out of the string concatenation and do the math first

"Welcome " + newCustomer + ", you are number " +  (array.indexOf(newCustomer) + 1 ) + " in line.";

Since you're using array.splice to insert at the 1st index position always, you could also remove all the array.indexOf and always simply output 1 though.

You could unshift the new client and take the retuend new lenght of the array as value.

 function clientQue(array, newCustomer) { return "Welcome " + newCustomer + ", you are number " + array.unshift(newCustomer) + " in line."; } let clients = ['Smith', 'Stebve', 'John'] console.log(clientQue(clients, 'Bob')); 

This will work for you (I always like using `` when concating strings

let clients = ['Smith', 'Stebve', 'John']

    function clientQue(array, newCustomer) {
        array.splice(1, 0, newCustomer)
        return `Welcome ${newCustomer} you are number ${parseInt(array.indexOf(newCustomer)) + 1}  in line.`;
    }
    let message = clientQue(clients, 'Bob');

    console.log(message)

This is the output

Welcome Bob you are number 2 in line.

Remove the quotes in this part from newCustomer. Needs to be evaluated to 'Bob'. parseInt(array.indexOf(newCustomer))

let clients = ['Smith', 'Stebve', 'John']

function clientQue(array, newCustomer) {
    return "Welcome " + newCustomer + ", you are number " +  (parseInt([...array, newCustomer].indexOf(newCustomer)) + 1) + " in line.";
}
clientQue(clients, 'Bob');

This should return what you need, however Array.splice mutates the original array, which means it also modifies the values of the original array. It can be harmful to change values of the given parameter.

[...array]

creates a new array from the old array's elements and

[...array, newCustomer]

pushes the new element inside the new array.

Reposting my answer since it was deleted and I wasn't given a chance to update it

A couple things:

Calling .splice removes an element from the array, and based on your question it doesn't seem like that is the desired result.

Furthermore, you don't need to call array.indexOf() because the length of the array will be the position of the newly added client.

You can have a function that takes a name such as "Jeff", adds it to the client array, and then returns the welcome message. Here is an example.

 var clients = ["bob", "jane", "isaac", "harry"]; function addClient(name) { clients.push(name); return "Welcome " + name + " you are position " + clients.length + " in array"; } console.log(addClient("Jeff")); 

  1. As clients is a queue, I think you would like to put the new customer to the front of the array, so you should use index 0 in splice . ie

    array.splice(0, 0, newCustomer);

    If you would like to put the newCustomer at the back, you may use push() instead. (I prefer using push() actually.)

  2. array.indexOf() should return a number, so you do not need to use parseInt() .

  3. However, as you are using string operations before incrementing the index, you should use parenthesis for the addition operation. ie

     'Welcome ' + newCustomer + ', you are number ' + (array.indexOf(newCustomer) + 1) + ' in line.' 

Note: As Bob is the new customer at the end of the queue, you may need to change the index calculation to: (array.length - array.indexOf(newCustomer)) instead.

Let's put them together,

let clients = ['Smith', 'Stebve', 'John'];

function clientQue(array, newCustomer) {
  array.splice(0, 0, newCustomer);
  return (
    'Welcome ' +
    newCustomer +
    ', you are number ' +
    (array.indexOf(newCustomer) + 1) +
    ' in line.'
  );
}
const q = clientQue(clients, 'Bob');

console.log(q); // Welcome Bob, you are number 1 in line.

console.log(clients); // [ 'Bob', 'Smith', 'Stebve', 'John' ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM