[英]Finding items from an array of objects based on a selected item value in JS
我有一個這樣的數組:
var array = [
{ name:"string123", value:"this", other: "that" },
{ name:"string2", value:"this", other: "that" },
{ name:"string12", value:"ths", other: "that" },
];
現在我有一個像'string12'這樣的字符串。 現在有了這個字符串 output 我想要的是找到包含這個字符串的項目,就像:
var array = [
{ name:"string123", value:"this", other: "that" },
{ name:"string12", value:"ths", other: "that" },
];
這是我嘗試過的:
const search = (nameKey, myArray) => { for (var i = 0; i < myArray.length; i++) { var newArr = []; if (myArray[i].name.slice(0, nameKey.length + 1) === nameKey) { newArr.push(myArray[i]); } return newArr; } } var array = [ { name:"string123", value:"this", other: "that" }, { name:"string2", value:"this", other: "that" }, { name:"string12", value:"ths", other: "that" }, ]; var resultObject = search("string12", array); console.log(resultObject);
但我的解決方案是返回一個空白數組。 我哪里錯了?
您的實施中出現以下問題:
for
循環之外。.slice()
時不需要 +1。return newArr
應該在for
循環之外。const search = (nameKey, myArray) => {
var newArr = [];
for (var i = 0; i < myArray.length; i++) {
if (myArray[i].name.slice(0, nameKey.length) === nameKey) {
newArr.push(myArray[i]);
}
}
return newArr;
}
var array = [
{ name:"string123", value:"this", other: "that" },
{ name:"string2", value:"this", other: "that" },
{ name:"string12", value:"ths", other: "that" },
];
var resultObject = search("string12", array);
console.log(resultObject);
使用Array#filter
和String#includes
:
const arr = [ { name:"string123", value:"this", other: "that" }, { name:"string2", value:"this", other: "that" }, { name:"string12", value:"ths", other: "that" } ]; const res = arr.filter(({ name }) => name.includes('string12')); console.log(res);
如果您只需要檢查名稱是否以字符串開頭,則可以使用String#startsWith
而不是includes
。
您可以使用Array.filter
和startsWith
function ( docs ) 來執行此操作。 如果您想在name
中的任何位置檢查string12
,請使用includes
而不是startsWith
var array = [ { name:"string123", value:"this", other: "that" }, { name:"string2", value:"this", other: "that" }, { name:"string12", value:"ths", other: "that" }, ]; const filtered = array.filter(item => item.name.startsWith("string12")); console.log(filtered);
如果您只是在開始時搜索字符串,那么從myArray[i].name.slice(0,nameKey.length + 1) 中刪除 + 1將對您有用,那么只有它會匹配您正在搜索的確切字符串並且您還必須在循環上方聲明 newArr 以便它可以保存該值並在循環結束后返回它希望它有幫助
const search = (nameKey, myArray)=> {
var newArr = [];
for (var i=0; i < myArray.length; i++) {
if (myArray[i].name.slice(0,nameKey.length) === nameKey) {
newArr.push(myArray[i]);
}
}
return newArr;
}
var array = [
{ name:"string123", value:"this", other: "that" },
{ name:"string2", value:"this", other: "that" },
{ name:"string12", value:"ths", other: "that" },
{ name:"string1234567", value:"ths", other: "that" },
];
var resultObject = search("string12", array);
console.log(resultObject);
你也可以使用現代語法來解決這個問題
const search = (nameKey, myArray) => {
return myArray.filter(elem => elem.name.startsWith(nameKey));
};
var array = [
{ name: "string123", value: "this", other: "that" },
{ name: "string2", value: "this", other: "that" },
{ name: "string12", value: "ths", other: "that" },
];
var resultObject = search("string12", array);
console.log(resultObject);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.