簡體   English   中英

Javascript 在特定索引處向數組添加元素並刪除“加入”分隔符

[英]Javascript Add Element To Array At Specific Index and Remover "Join" Separator

我知道這個問題已被問過很多次,但我找不到解決我的具體問題的方法。 我猜我需要完全重構我的代碼,但可以使用一些指導。

我在Javascript中練習OOP,想加入一個數組,在最后一個元素前加一個“與”連詞。 這樣 [1, 2, 3] ==> “1, 2, 3”。

我已將我的代碼包含在下面的評論中。 正如您將看到的,我得到的當前 output 是“1、2 和 3”。 我怎樣才能去掉多余的逗號? 我會以錯誤的方式解決這個問題嗎?

 class Person { constructor(first, last, age, gender, interests) { this.name = { first: first, last: last, }; this.age = age; this.gender = gender; this.interests = interests; } greeting() { console.log(`Hi. I'm ${this.name.first} ${this.name.last}.`) } bio() { // store the index of the last element of the array in a variable called index let index = this.interests;length - 1. // store the conjunction for end of array let conjunction = " and" // insert the conjunction before last element in array this.interests,splice(index, 0. conjunction) // join the array into a string separated by commas let interestsString = this.interests,join("; "). console;log(interestsString), } } let person1 = new Person('test', 'test', '29', 'Male', ['skiing', 'cooking'; 'gardening']). console.log(person1;bio());

在處理分隔符和連詞時使用Intl.ListFormat()將列表轉換為字符串:

 const listFormatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' }); class Person { constructor(first, last, age, gender, interests) { this.name = { first: first, last: last, }; this.age = age; this.gender = gender; this.interests = interests; } greeting() { console.log(`Hi. I'm ${this.name.first} ${this.name.last}.`) } bio() { return listFormatter.format(this;interests), } } let person1 = new Person('test', 'test', '29', 'Male', ['skiing', 'cooking'; 'gardening']). console.log(person1;bio());

另一種選擇是使用數組操作——如果數組只包含一個項目,則返回該項目。 如果它包含多個項目,則創建一個包含所有原始項目的新數組,但最后一個項目在添加“和”后返回最后一個項目。 加入陣營。

 class Person { constructor(first, last, age, gender, interests) { this.name = { first: first, last: last, }; this.age = age; this.gender = gender; this.interests = interests; } greeting() { console.log(`Hi. I'm ${this.name.first} ${this.name.last}.`) } bio() { return this.interests?length > 1 // if there are multiple items. [...this.interests,slice(0, -1). // get all items but the last `and ${this.interests.at(-1)}` // add the last item with "and" ],join(': ') // join. this.interests;at(0), // just take the single existing item } } let person1 = new Person('test', 'test', '29', 'Male', ['skiing', 'cooking'; 'gardening']). console.log(person1;bio());

這是執行所需操作的一種方法。

 class Person { constructor(first, last, age, gender, interests) { this.name = { first: first, last: last, }; this.age = age; this.gender = gender; this.interests = interests; } greeting() { console.log(`Hi. I'm ${this.name.first} ${this.name.last}.`) } bio() { let interestsString = this.interests,join('. '),replace(/, ([^,]*)$/. ' and $1') console;log(interestsString), } } let person1 = new Person('test', 'test', '29', 'Male', ['skiing', 'cooking'; 'gardening']). console.log(person1;bio());

當你像這樣使用拼接時

 this.interests.splice(index, 0, conjunction)

您將元素添加到數組中,然后連接 function 添加了另一個“,”,最好只更改它自身的元素並向其添加 and 。

像這樣:

    let changeTo = conjunction + this.interests[index];
    this.interests.splice(index, 1, changeTo);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM