[JS Array]Find an item in an array that contains a string and then return that string
2018-03-21 12:40
681 查看
[A JavaScript question from stackoverflow.com]
question:
I have an array of strings.
I want to search in that array for and string that contains a specific string.
If it's found, return that string WITHOUT the bit of the string we looked for.
So, the array has three words. "Strawbery", "Lime", "Word:Word Word"
I want to search in that array and find the full string that has "Word:" in it and return "Word Word"
[译]
有一个字符串数组,查找含有指定字符串的项,返回该项中不包含指定字符串的字符串
例如一个数组中包含 “Strawbery”“Lime”“Word:Word Word”,搜索包含字符串“Word:”的项,然后返回“Word Word”。
answer:
You can use
This code will return the value of you want only.
[译]使用find()方法遍历数组,然后使用replace()方法移除不需要的字符串,即可得到你想要的字符串。let arr = ["Strawbery", "Lime", "Word:Word Word"];
let search = "Word:";
let result = (arr.find(e => e.includes(search)) || "").replace(search, '');
console.log(result);if there are multiple search results, you can use
[译]假如有重复的搜索结果,使用filter()和map()。
let arr = ["Strawbery", "Word:Lime", "Word:Word Word"];
let search = "Word:";
let result = arr.filter(e => e.includes(search)).map(e => e.replace(search, ''));
console.log( result );
question:
I have an array of strings.
I want to search in that array for and string that contains a specific string.
If it's found, return that string WITHOUT the bit of the string we looked for.
So, the array has three words. "Strawbery", "Lime", "Word:Word Word"
I want to search in that array and find the full string that has "Word:" in it and return "Word Word"
[译]
有一个字符串数组,查找含有指定字符串的项,返回该项中不包含指定字符串的字符串
例如一个数组中包含 “Strawbery”“Lime”“Word:Word Word”,搜索包含字符串“Word:”的项,然后返回“Word Word”。
answer:
You can use
findto search the array. And use
replaceto remove the string.
This code will return the value of you want only.
[译]使用find()方法遍历数组,然后使用replace()方法移除不需要的字符串,即可得到你想要的字符串。let arr = ["Strawbery", "Lime", "Word:Word Word"];
let search = "Word:";
let result = (arr.find(e => e.includes(search)) || "").replace(search, '');
console.log(result);if there are multiple search results, you can use
filterand
map
[译]假如有重复的搜索结果,使用filter()和map()。
let arr = ["Strawbery", "Word:Lime", "Word:Word Word"];
let search = "Word:";
let result = arr.filter(e => e.includes(search)).map(e => e.replace(search, ''));
console.log( result );
相关文章推荐
- Find the repeated item in an array
- how to remove nil and blank string in an array in Ruby
- Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
- pointer to ref of an array, and to the func that return a ref of an array
- [TypeScript] Find the repeated item in an array using TypeScript
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- You’re given an array containing both positive and negative integers and required to find the sub-a
- An instance of getting the maximum and minimum values in an array
- Promblem 442 Find All Duplicates in an Array
- stackover flow 上的一个问题:Find the most common entry in an array
- Facebook面试题 find the peek or valley index in an array
- Find All Numbers Disappeared in an Array
- 【LeetCode】Find Minimum In Rotated Sorted Array 1 and 2
- LeetCode Find All Numbers Disappeared in an Array
- leetcode448~Find All Numbers Disappeared in an Array
- Find All Duplicates in an Array
- Find the Smallest K Elements in an Array
- LeetCode-Find All Duplicates in an Array & Find All Numbers Disappeared in an Array
- Find All Numbers Disappeared in an Array
- find-all-numbers-disappeared-in-an-array