2017-02-11 104 views
-1

我有一個對象數組,我想搜索每個對象,並檢查數據是否匹配給定的值,我想返回數組中的對象,如果沒有找到,那麼我必須返回undefined。如何從數組中返回一個對象?

這是我到目前爲止有:

var data = [{ 
    id: 1, 
    firstName: 'John', 
    lastName: 'Smith' 
}, { 
    id: 2, 
    firstName: 'Jane', 
    lastName: 'Smith' 
}, { 
    id: 3, 
    firstName: 'John', 
    lastName: 'Doe' 
}]; 
var asf[] = data[0]; 
return asf; 

我試圖返回對象,如果條件中,如果else語句的比賽,但它給我的錯誤在返回數組對象。

我也試圖使用_.findwhere(data, pid)它是下劃線模塊中的方法我可以使用它來返回對象數組?

+0

你是什麼意思「迴歸n對象不在數組中「。你想刪除它嗎? – Carcigenicate

+0

var findObjectWithThisValue = 3; VAR數據= [{ ID:1, 姓: '約翰', 名字: '史密斯' },{ ID:2, 姓:'簡, 名字: '史密斯' },{ id:3, 姓氏:'John', 姓氏:'Doe' }]; function SearchObject(value){ var myObject = data.filter(function(obj){ return obj.id == findObjectWithThisValue }); if(myObject.length == 0){ return'undefined'; } else { return myObject; } } SearchObject(findObjectWithThisValue); –

+0

如果有多個比賽怎麼辦? – 2017-02-11 18:29:48

回答

0

我不知道,如果你想回報對象或刪除對象,所以我會告訴你如何做到既因爲它們都是很簡單的事情。

這是您的數據的整理版:

// this is your data 
var data = [{ 
    id: 1, 
    firstName: 'John', 
    lastName: 'Smith' 
}, { 
    id: 2, 
    firstName: 'Jane', 
    lastName: 'Smith' 
}, { 
    id: 3, 
    firstName: 'John', 
    lastName: 'Doe' 
}]; 

這你會用它來回報從陣列目標對象的循環:

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    return data[i]; 
    } 
    // continue with the loop if the current item is not "John Smith" 
    continue; 
} 

這個片段做確切同樣的東西,但沒有continue

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    return data[i]; 
    } 
} 

這你會使用到循環從數組中刪除目標對象:

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    delete data[i]; 
    // you can also use Array.prototype.splice() to remove an item from an array: 
    // data.splice(i, 1); 
    } 
    // continue with the loop if the current item is not "John Smith" 
    continue; 
} 

這個片段做同樣的事情,但沒有continue

// loop through the data array 
for(var i = 0; i < data.length; i++) { 
    // check if the current item is "John Smith" 
    if(data[i].firstName == "John" && data[i].lastName == "Smith") { 
    delete data[i]; 
    // you can also use Array.prototype.splice() to remove an item from an array: 
    // data.splice(i, 1); 
    } 
} 

使用此代碼片段,如果你正在使用jQuery,而不是返回或刪除任何你可以處理該對象,但是你請在jQuery回調函數中。
在這種情況下,我將使用console.log();爲例:

$.each(data, function(i, object) { 
    if(object.firstName == "John" && object.lastName == "Smith") { 
    console.log(object); 
    } 
}); 

祝你好運,萬事如意。

+0

當我將對象從數組中返回時,我需要使另一個數組接受這個傳入對象,因爲我想進一步操作。像var asf [] =函數(數據) –

+0

@KamranSaeed不,你不知道。例如,它說'console.log(object);'你可以改變它來指定對象的任何功能或以任何你需要的方式操縱它。 – 2017-02-11 17:34:53

+0

「繼續」陳述的目的是什麼? – 2017-02-11 17:37:18

1

您可以使用Array.prototype.find(),像這樣:

var data = [{ 
    id: 1, 
    firstName: 'John', 
    lastName: 'Smith' 
}, { 
    id: 2, 
    firstName: 'Jane', 
    lastName: 'Smith' 
}, { 
    id: 3, 
    firstName: 'John', 
    lastName: 'Doe' 
}]; 

data.find(x => {x.id === 1}); 

如果您想了解更多關於陣列訪問次數此鏈接。

http://exploringjs.com/es6/ch_arrays.html

0

香草JS:

var findWhere = function(key,val,array) { 
    var o; 
    array.some(function(object) { return object[key] == val && (o = object); }); 
    return o; 
}; 

var data = []; //your array 
findWhere('id',1,data); //get the first object in the array where id = 1 

編輯

這裏有一個更好的,實際上需要一個回調,並且可以返回只有一個元素,或所有匹配的元素:

var find = function(arr,cb,all) { 
    var o; 
    if(all) { 
     return arr.filter(cb); 
    } 

    arr.some(function(object) { return cb(object) && (o = object); }); 
    return o; 
}; 

var findAll = function(arr,cb) { return find(arr,cb,true); }; 

//return the first object in the data array where the id = 1 
find(data,function(object) { return object.id == 1 }); 

//return all objects in the data array where name = 'John' 
findAll(data,function(object) { return object.firstName = 'John'; }); 
+0

這只是寫'find'的一種尷尬的方式。 – 2017-02-11 17:38:15

+0

有更好的瀏覽器支持的尷尬的方式,是 – Adam

+0

@Adam你介意我是否用我的網站替換了我的網站上的'find'多填充?它更簡單,更易於理解。 – 2017-02-11 18:33:18