如何在Array javascript中引用元素的位置。 (我没有得到预期的结果)

问题描述 投票:0回答:7

我想要实现的是,无论何时将新客户端添加到客户端数组,我希望函数返回“欢迎(新客户名称),你是(他在数组中的位置)。”

我究竟做错了什么?我试图让新客户端的索引添加1,以便从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');
javascript arrays concatenation
7个回答
2
投票

您缺少括号()以突破字符串连接并首先进行数学运算

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

由于你总是使用array.splice插入第一个索引位置,你也可以删除所有的array.indexOf,但总是只输出1


1
投票

您可以取消移动新客户端并将返回的新数组长度作为值。

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

1
投票

这对你有用(我总是喜欢在串联时使用``

让客户= ['史密斯','史蒂夫','约翰']

    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)

这是输出

欢迎鲍勃,你排在第2位。


0
投票

从newCustomer中删除此部分中的引号。需要评估'鲍勃'。 parseInt(array.indexOf(newCustomer))


0
投票
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');

这应该返回您需要的内容,但是Array.splice会改变原始数组,这意味着它还会修改原始数组的值。更改给定参数的值可能是有害的。

[...array]

从旧数组的元素创建一个新数组

[...array, newCustomer]

将新元素推送到新数组中。


0
投票

重新发布我的答案,因为它被删除,我没有机会更新它

几件事:

调用.splice会从数组中删除一个元素,并根据您的问题,这似乎不是理想的结果。

此外,您不需要调用array.indexOf(),因为数组的长度将是新添加的客户端的位置。

您可以使用一个名为“Jeff”的函数,将其添加到客户端数组,然后返回欢迎消息。这是一个例子。

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

0
投票
  1. 由于客户端是队列,我认为您希望将新客户放在数组的前面,因此您应该在splice中使用索引0。即 array.splice(0, 0, newCustomer); 如果您想将newCustomer放在后面,可以使用push()代替。 (我更喜欢使用push()。)
  2. array.indexOf()应该返回一个数字,所以你不需要使用parseInt()
  3. 但是,由于在递增索引之前使用字符串操作,因此应使用括号进行加法运算。即 'Welcome ' + newCustomer + ', you are number ' + (array.indexOf(newCustomer) + 1) + ' in line.'

注意:由于Bob是队列末尾的新客户,您可能需要将索引计算更改为:(array.length - array.indexOf(newCustomer))

我们把它们放在一起,

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' ]
© www.soinside.com 2019 - 2024. All rights reserved.