通过jQuery中的特定值从对象获取对象

时间:2018-02-19 11:22:53

标签: javascript

我有一个像这样的对象。

var obj = 
{
    0:{submissionID: "136169", date: "2018-02-17 14:40:00", compId: "j1hrxej5", browser: "Chrome", formName: "Name1"}
    1:{submissionID: "6113", date: "2018-02-17 12:55:13", compId: "j1hrxej5", browser: "Mozila", formName: "Name2"}
    2:{submissionID: "500", date: "2018-02-16 18:45:58", compId: "jhg2l9lj", browser: "Opera", formName: "Name3"}
    3:{submissionID: "306", date: "2018-02-16 18:43:45", compId: "j372155lj", browser: "Chrome", formName: "Name4"}
    4:{submissionID: "50150", date: "2018-02-16 18:41:36", compId: "j37189lj", browser: "Opera", formName: "Name5"}
};

我可以通过它的submissionID获取一个对象吗?像SQL一样。首先,我创建了这样的对象。

var obj = 
{
    136169:{submissionID: "136169", date: "2018-02-17 14:40:00", compId: "j1hrxej5", browser: "Chrome", formName: "Name1"}
    6113:{submissionID: "6113", date: "2018-02-17 12:55:13", compId: "j1hrxej5", browser: "Mozila", formName: "Name2"}
    500:{submissionID: "500", date: "2018-02-16 18:45:58", compId: "jhg2l9lj", browser: "Opera", formName: "Name3"}
    306:{submissionID: "306", date: "2018-02-16 18:43:45", compId: "j372155lj", browser: "Chrome", formName: "Name4"}
    50150:{submissionID: "50150", date: "2018-02-16 18:41:36", compId: "j37189lj", browser: "Opera", formName: "Name5"}
}; 

然后得到obj [submissionID],但我现在必须修复我的代码

4 个答案:

答案 0 :(得分:1)

使用Object.valuesfind

var idToFind = "500";
var output = Object.values( obj ).find( s => s.id == idToFind );

答案 1 :(得分:0)

您可以使用数组而不是对象,如下所示:

var obj = 
[
    {submissionID: "136169", date: "2018-02-17 14:40:00", compId: "j1hrxej5", browser: "Chrome", formName: "Name1"}
    {submissionID: "6113", date: "2018-02-17 12:55:13", compId: "j1hrxej5", browser: "Mozila", formName: "Name2"}
    {submissionID: "500", date: "2018-02-16 18:45:58", compId: "jhg2l9lj", browser: "Opera", formName: "Name3"}
    {submissionID: "306", date: "2018-02-16 18:43:45", compId: "j372155lj", browser: "Chrome", formName: "Name4"}
    {submissionID: "500", date: "2018-02-16 18:41:36", compId: "j37189lj", browser: "Opera", formName: "Name5"}
];

然后,创建一个过滤函数:

var specificID = "500";
val filteredArray = array.filter(function(element) {
    return (element.submissionID === specificID)
};
// filteredArray[0] is the object that you are looking for

有关过滤方法的更多详细信息,请参见here

答案 2 :(得分:0)

对于您的回复,请尝试以下方法:

var obj = 
{
    0:{submissionID: "136169", date: "2018-02-17 14:40:00", compId: "j1hrxej5", browser: "Chrome", formName: "Name1"},
    1:{submissionID: "6113", date: "2018-02-17 12:55:13", compId: "j1hrxej5", browser: "Mozila", formName: "Name2"},
    2:{submissionID: "500", date: "2018-02-16 18:45:58", compId: "jhg2l9lj", browser: "Opera", formName: "Name3"},
    3:{submissionID: "306", date: "2018-02-16 18:43:45", compId: "j372155lj", browser: "Chrome", formName: "Name4"},
    4:{submissionID: "50150", date: "2018-02-16 18:41:36", compId: "j37189lj", browser: "Opera", formName: "Name5"}
};

如果这是回复,请使用以下方法获得所需答案:

for (var i = Object.keys(obj).length - 1; i >= 0; i--) {
     if (obj[i].submissionID==="500") {
        var obj2 = obj[i];
        console.log(obj2);
        }
}

答案 3 :(得分:0)

您可以使用$(function () { var chart = new Highcharts.Chart({ chart: { renderTo: "container", type: "column", marginLeft: 85, marginRight: 15 }, title: { useHTML: true, x: 10, y: 80, text: '' }, series: [{ data: [4, 14, 18, 5, 6, 5, 14, 15, 18] }], xAxis: { style:{ overflow: 'none', crop : false }, min:0, opposite:true, labels: { style: { textOverflow: 'none', whiteSpace: 'nowrap', color: '#000000', fontSize: '11px', overflow: 'none', crop : false, }, gridLineWidth: 1, groupedOptions: [ { rotation: 0, // rotate labels for a 2nd-level y:10, }], rotation: -90 // 0-level options aren't changed, use them as always }, categories: [{ name: "Fruiqweqw ewqeqwet", categories: ["Appleasd asdadd", "Bananaasd asdsa", "Orange asdad"] }, { name: "Vegetableasdasd", categories: ["Carrot asd", "Potato sdf", "Tomato"] }, { name: "Fishqwewewqewq", categories: ["Codasdsa", "Salmonasd", "Tuna"] }] } }); }); Object.keys的组合来获取地图。注意:事实证明,声明对象的语法不正确,因为它缺少逗号。

Array.reduce