Arl*_*ler 320 javascript arrays
我之前已经问过类似的问题,但这个问题有点不同.我有一个未命名的对象数组,其中包含一个命名对象数组,我需要获取"name"为"string 1"的对象.这是一个示例数组.
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
Run Code Online (Sandbox Code Playgroud)
更新:我应该早些说过,但是一旦找到它,我想用一个编辑过的对象替换它.
Šim*_*das 647
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find(o => o.name === 'string 1');
console.log(obj);Run Code Online (Sandbox Code Playgroud)
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find((o, i) => {
if (o.name === 'string 1') {
arr[i] = { name: 'new string', value: 'this', other: 'that' };
return true; // stop searching
}
});
console.log(arr);Run Code Online (Sandbox Code Playgroud)
Asc*_*iom 207
您可以遍历数组并测试该属性:
function search(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].name === nameKey) {
return myArray[i];
}
}
}
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
var resultObject = search("string 1", array);
Run Code Online (Sandbox Code Playgroud)
小智 135
在ES6中你可以Array.prototype.find(predicate, thisArg?)像这样使用:
array.find(x => x.name === 'string 1')
Run Code Online (Sandbox Code Playgroud)
http://exploringjs.com/es6/ch_arrays.html#_searching-for-array-elements https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find
然后替换所述对象(并使用另一个很酷的ES6方法fill),您可以执行以下操作:
let obj = array.find(x => x.name === 'string 1');
let index = array.indexOf(obj);
array.fill(obj.name='some new string', index, index++);
Run Code Online (Sandbox Code Playgroud)
小智 37
根据ECMAScript 6,您可以使用该findIndex功能.
array[array.findIndex(x => x.name == 'string 1')]
Run Code Online (Sandbox Code Playgroud)
小智 22
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
var foundValue = array.filter(obj=>obj.name==='string 1');
console.log(foundValue);
Run Code Online (Sandbox Code Playgroud)
Wal*_*ers 20
与underscore.js使用findWhere方法:
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
var result = _.findWhere(array, {name: 'string 1'});
console.log(result.name);
Run Code Online (Sandbox Code Playgroud)
Joã*_*lva 14
要么使用简单的for循环:
var result = null;
for (var i = 0; i < array.length; i++) {
if (array[i].name === "string 1") {
result = array[i];
break;
}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果可以,也就是说,如果您的浏览器支持它,请使用Array.filter,这更简洁:
var result = array.filter(function (obj) {
return obj.name === "string 1";
})[0];
Run Code Online (Sandbox Code Playgroud)
Gau*_*rma 14
考虑到您有以下代码段:
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
Run Code Online (Sandbox Code Playgroud)
您可以使用以下功能搜索项目
const search = what => array.find(element => element.name === what);
Run Code Online (Sandbox Code Playgroud)
您可以检查是否找到了该项目.
if (search("string 1")) {
console.log(search.value, search.other);
} else {
console.log('No result found');
}
Run Code Online (Sandbox Code Playgroud)
Dur*_*ngh 13
一行答案.您可以使用过滤功能来获得结果.
var array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
console.log(array.filter(function(arr){return arr.name == 'string 1'})[0]);Run Code Online (Sandbox Code Playgroud)
Mar*_*nVK 11
随着foreach:
let itemYouWant = null;
array.forEach((item) => {
if (item.name === 'string 1') {
itemYouWant = item;
}
});
console.log(itemYouWant);
Run Code Online (Sandbox Code Playgroud)
Sim*_*bæk 10
我添加了prop作为参数,使其更通用和可重用
/**
* Represents a search trough an array.
* @function search
* @param {Array} array - The array you wanna search trough
* @param {string} key - The key to search for
* @param {string} [prop] - The property name to find it in
*/
function search(array, key, prop){
// Optional, but fallback to key['name'] if not selected
prop = (typeof prop === 'undefined') ? 'name' : prop;
for (var i=0; i < array.length; i++) {
if (array[i][prop] === key) {
return array[i];
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var array = [
{
name:'string 1',
value:'this',
other: 'that'
},
{
name:'string 2',
value:'this',
other: 'that'
}
];
search(array, 'string 1');
// or for other cases where the prop isn't 'name'
// ex: prop name id
search(array, 'string 1', 'id');
Run Code Online (Sandbox Code Playgroud)
摩卡测试:
var assert = require('chai').assert;
describe('Search', function() {
var testArray = [
{
name: 'string 1',
value: 'this',
other: 'that'
},
{
name: 'string 2',
value: 'new',
other: 'that'
}
];
it('should return the object that match the search', function () {
var name1 = search(testArray, 'string 1');
var name2 = search(testArray, 'string 2');
assert.equal(name1, testArray[0]);
assert.equal(name2, testArray[1]);
var value1 = search(testArray, 'this', 'value');
var value2 = search(testArray, 'new', 'value');
assert.equal(value1, testArray[0]);
assert.equal(value2, testArray[1]);
});
it('should return undefined becuase non of the objects in the array have that value', function () {
var findNonExistingObj = search(testArray, 'string 3');
assert.equal(findNonExistingObj, undefined);
});
it('should return undefined becuase our array of objects dont have ids', function () {
var findById = search(testArray, 'string 1', 'id');
assert.equal(findById, undefined);
});
});
Run Code Online (Sandbox Code Playgroud)
检测结果:
Search
? should return the object that match the search
? should return undefined becuase non of the objects in the array have that value
? should return undefined becuase our array of objects dont have ids
3 passing (12ms)
Run Code Online (Sandbox Code Playgroud)
如果你想知道为什么这是不好的做法,那么看看这篇文章:
进行数组搜索的原型版本:
Array.prototype.search = function(key, prop){
for (var i=0; i < this.length; i++) {
if (this[i][prop] === key) {
return this[i];
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
var array = [
{ name:'string 1', value:'this', other: 'that' },
{ name:'string 2', value:'this', other: 'that' }
];
array.search('string 1', 'name');
Run Code Online (Sandbox Code Playgroud)
你可以用一个简单的循环来做到这一点:
var obj = null;
for (var i = 0; i < array.length; i++) {
if (array[i].name == "string 1") {
obj = array[i];
break;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
576255 次 |
| 最近记录: |