从js数据对象中检索数据

时间:2014-01-21 17:47:12

标签: javascript

假设我正在检索包含用户信息的JSON对象。我收到的其中一条信息是代码,表明他们最喜欢的水果。

所以我有 userData.faveFruit ,我知道它的价值将是 APP (苹果), BAN (香蕉), CHE (樱桃),或 KIW (奇异果)。我需要将数据放在页面上,其中包含有关用户最喜欢的水果的详细信息。

所以我创建了这个数据对象:

var fruitDefinitions = [
    { "APP" : [ "Apple",  "Red, green or yellow skin, round, white flesh, hard core with seeds." ] },
    { "BAN" : [ "Banana", "Thick yellow skin, elongated curved cylinder tapered at both ends, white flesh." ] },
    { "CHE" : [ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ] },
    { "KIW" : [ "Kiwi",   "Brown furry skin, oval, green flesh, many small edible seeds." ] },
];

那么我如何从 CHE 变为能够拔出 Cherry 及其描述?它似乎应该很简单,但我做的每一次尝试都不起作用。我应该以不同方式构建 fruitDefinitions 吗?

感谢。

6 个答案:

答案 0 :(得分:2)

我建议稍微重构一下你的数据:

var fruitDefinitions = {
    "APP" : [ "Apple",  "Red, green or yellow skin, round, white flesh, hard core with seeds." ],
    "BAN" : [ "Banana", "Thick yellow skin, elongated curved cylinder tapered at both ends, white flesh." ],
    "CHE" : [ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ],
    "KIW" : [ "Kiwi",   "Brown furry skin, oval, green flesh, many small edible seeds." ]
};

如此使用:

console.log(fruitDefinitions.CHE[0]); // Cherry
console.log(fruitDefinitions.CHE[1]); // Red skin and flesh, round, green stem, single inedible pit.

答案 1 :(得分:1)

使用.filter方法:

使用不同数据的示例:

var matches = [ { foo : 'bar' }, { zoo : 'zar' } ].filter(function(item) {
  return item.hasOwnProperty('zoo');
});

console.log(matches[0]) // should be { zoo : 'zar' }

您也可以像这样存储您的对象:

var fruitDefinitions = {
     "APP" : [ "Apple",  "Red, green or yellow skin, round, white flesh, hard core with seeds." ] ,
     "BAN" : [ "Banana", "Thick yellow skin, elongated curved cylinder tapered at both ends, white flesh." ] ,
     "CHE" : [ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ] ,
     "KIW" : [ "Kiwi",   "Brown furry skin, oval, green flesh, many small edible seeds." ] ,
};

因此您可以像这样访问您的数据:fruitDefinitions['BAN']

答案 2 :(得分:0)

这只是一个 foreach 循环。不要将foreach循环用于数组。

使用Array.filter方法作为lxe建议。

答案 3 :(得分:0)

可能做得更紧凑,但作为第一种方法:

function findfavFruit(fCode) {
    for ( var n = 0, n < fruitDefinitions.length; n++ ) {
          if ( typeof fruitDefinitions[n][fCode] != undefined )
               return fruitDefinitions[n][fCode];
    }
    return null;
}

这将返回一个数组

[ "Cherry", "Red skin and flesh, round, green stem, single inedible pit." ]

表示“CHE”

答案 4 :(得分:0)

如果你真的想要完全重新组织它,那就远离那些JSON的东西并做一个完整的OO设计,它实际上就是在哭泣:

var fruit = function(code, name, desc) {
    this.name = name;
    this.code = code;
    this.description = desc;

    this.getName = function() { return this.name }
    ....
}

var fruits = function() {

    this.fav = []
    this.add = function(name, desc, code) {
        var f = new fruit(code, name, desc);
        this.fav.push(f);

    ....
    etc. etc.

答案 5 :(得分:-1)

访问fruites名称

fruitDefinitions[0]['APP'][0]

访问水果描述

  fruitDefinitions[0]['APP'][1]

如果您的输入是动态的,那么您可以分别使用下面的水果名称和描述代码。

 fruitDefinitions[0][input][0]

 fruitDefinitions[0][input][1]

当你的数组fruitDefinitions多个值时,那么将有数组的索引号而不是'0',这意味着你的结构将是

       fruitDefinitions[i]['APP'][1]
       ......

这里'i'是数组的索引号

但如果结构如lxe所说那将是好的