te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Access object by name in array list - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access object by name in array list - Stack Overflow

programmeradmin4浏览0评论

I have an object that contains an array list of objects. I'd like to get the value of an object within the array list.

example

var data = { 
             items1: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }], 
             items2: [{ id: 3, name: 'foo' }, { id: 4, name: 'bar' }] 
           };

I'm trying to access the name of id:1 in array list items1.

I thought it would be something like

data['items1']['id'].name 

but I think I'm missing something. Anybody know what I might be doing wrong

I have an object that contains an array list of objects. I'd like to get the value of an object within the array list.

example

var data = { 
             items1: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }], 
             items2: [{ id: 3, name: 'foo' }, { id: 4, name: 'bar' }] 
           };

I'm trying to access the name of id:1 in array list items1.

I thought it would be something like

data['items1']['id'].name 

but I think I'm missing something. Anybody know what I might be doing wrong

Share Improve this question edited Sep 22, 2013 at 6:17 Kelsadita 1,0388 silver badges22 bronze badges asked Sep 22, 2013 at 5:57 Code JunkieCode Junkie 7,78827 gold badges86 silver badges146 bronze badges 3
  • For starters, that appears to be invalid json, given you don't have a ma after the first item. – Daedalus Commented Sep 22, 2013 at 6:00
  • Feel free to correct the issue you see, I just quickly modified the post from another example on the web to try and demonstrate what I'm trying to do. – Code Junkie Commented Sep 22, 2013 at 6:02
  • Not going to. One never modifies the code in the OP's question, as that changes the meaning. If the code is faulty, or they misposted, it is their job to fix/correct it. – Daedalus Commented Sep 22, 2013 at 6:04
Add a ment  | 

4 Answers 4

Reset to default 5

This is an object with 2 keys (items1 and items2), both of which are arrays. Within each array are elements which are objects, each containing 2 keys (id and name).

To get the id of the first element of the items1 array you would do:

data.items1[0].id

which would return 1.

If you wanted to search for the object with a name of 'bar' in items2 you could do something like:

function find(item, name) {
    //no such array
    if(!data[item])
        return;

    //search array for key
    var items = data[item];
    for(var i = 0; i < items.length; ++i) {
        //if the name is what we are looking for return it
        if(items[i].name === name)
            return items[i];
    }
}

var obj = find('items2', 'bar');
obj.id; //4
obj.name; //'bar'

I highly suggest reading about JavaScript Objects and Arrays.

You can only access array items by their numeric index. For example:

// The first item in the array
data['items1'][0].name
// The second
data['items1'][1].name

If you want to lookup by id, you can make a little function to do that for you:

function getItemById(anArray, id) {
    for (var i = 0; i < anArray.length; i += 1) {
        if (anArray[i].id === id) {
            return anArray[i];
        }
    }
}

var theName = getItemById(data['items1'], 1).name;

As items1 is array, you should write:

data.items1[0].name

Try this data['items1'][0].name

发布评论

评论列表(0)

  1. 暂无评论