2017-06-28 70 views
0

對javascript真的很陌生,希望能夠幫助我解決一個im問題。在數組中查找JavaScript對象的值

所以我基本上有一個存儲對象的數組。每個對象都包含一個id和一個變量i,它是一個數字。我的問題是這樣的:我怎麼能從對象數組中提取id的值?我正在使用的id已經被存儲在數組中,並帶有一個i值。

var i = 1; 
var id; 
var b = {}; 
var y = []; 

if(condition) { 

    b = {"123":i}; 

    y.push(b); 

} 

if(condition) { 
    id = 123; 
    //Find corresponding i value for id "123" from object array y 
    i = ?; 
} 
+4

的[在JavaScript對象數組查找ID對象]可能的複製(https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of- javascript-objects) –

+0

建議的解決方案適用於jQuery。他們也會爲JavaScript工作嗎? @JaredSmith – user3702643

+1

[Array#find](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Xotic750

回答

0

只需使用ObjectName[Key]這是到足以讓你的價值 像b[123]

0

很多方法可以做到這一點。這是其中之一。

var arr = [{id:1},{id:123}]; 
 

 
var obj = arr.filter(function(val){ 
 
    if(val.id===123) 
 
    return val 
 

 
}) 
 

 
console.log(obj,'obj')

+0

我得到的這個輸出是[]'obj'。任何想法爲什麼? – user3702643

+0

@ user3702643你運行了這個代碼片段嗎? – Ved

+0

我在我的本地環境中運行它。不能得到相同的結果。 – user3702643

-2

嘗試underscore.js(http://underscorejs.org),而與陣列處理的JS

+1

像下劃線這樣的庫是不需要的。這很簡單,使用[Array#find](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) –

0

你可以通過數組循環和獲取對象的屬性值如下:

var arr = [ 
 
    {"123": "valueA"}, 
 
    {"456": "valueB"} 
 
]; 
 

 
const id = "123"; 
 
let value; 
 

 
arr.some(obj => { 
 
    if (obj[id] || obj[id] === 0) value = obj[id]; 
 
}); 
 

 
console.log(value);

Documentation for "Array.some" method

+1

最好使用[Array#some]( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)而不是[Array#forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) – Xotic750

+0

好點,我已經更新了答案。 – abbotto

+0

如果'i'的值是'0'會怎麼樣? – Xotic750

1

Array#find

var hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty); 
 
var i = 1; 
 
var id; 
 
var b = {}; 
 
var y = []; 
 

 
var condition = true; 
 
if (condition) { 
 
    b = { 
 
    "123": i 
 
    }; 
 

 
    y.push(b); 
 
} 
 

 
if (condition) { 
 
    id = 123; 
 
    // Find corresponding i value for id "123" from object array y 
 
    // i = ? ; 
 
    var found = y.find(function(o) { 
 
    return hasOwn(o, id); 
 
    }); 
 
    var f = found ? found[id] : found; 
 
    console.log(f); 
 
}
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-shim.min.js"></script> 
 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-sham.min.js"></script> 
 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script> 
 
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.js"></script> 
 
<script type="text/javascript" src="https://wzrd.in/standalone/[email protected]"></script>

+0

儘管此答案確實找到「我」的值,但items數組與OP給出的示例不匹配。 – abbotto

+0

不,因爲這是在編輯Q之前發佈的。 :) – Xotic750

+0

在某些瀏覽器中可能會調用「hasOwnProperty」。 例如:https://stackoverflow.com/questions/8157700/object-has-no-hasownproperty-method-i-e-its-undefined-ie8 – abbotto

0
const stuff = [ 
    { 
    name: 'Leonardo', 
    id: 100 
    }, 
    { 
    name: 'Donatello', 
    id: 101 
    }, 
    { 
    name: 'Raphael', 
    id: 102 
    }, 
    { 
    name: 'Michaelangelo', 
    id: 103 
    }, 
]; 

首先一個例子中,使用陣列上的Array.prototype.find()方法找到具有所需的ID和存儲在其內的對象它在entry變量中。然後,在該對象內記錄對應於name鍵的值。

const desired = 102; 
const entry = stuff.find(item => item.id === desired); 

console.log(entry.name);