无法通过JSON数组迭代 [英] Unable to iterate through JSON array

查看:141
本文介绍了无法通过JSON数组迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API(中的NodeJS)反应,还给数组:

  res.json(朋友)
[
    {
        ID:7795239,
        用户名:janesmith
    },
    {
        ID:1363327,
        用户名:JohnSmith对
    }
]

但是,当我要发送此响应之前通过数组迭代,我找不到这样做的方式。
users.length 不存在,为(VAR关键用户)好好尝试一下工作无论是。

如何解决此问题?

编辑:code那里得到的数组:

  User.find({}功能(ERR,用户){
    如果(ERR)res.send(ERR);
    VAR的朋友;
    VAR的结果='这将保存结果;    对(在用户VAR键){
        若(网友[关键] .username == req.de coded.username){
            朋友们用户= [关键] .friends;
        }
    }    //这里我想要做的事情在朋友的列表,所以这是我需要遍历它的地方    res.json(结果);
});

这是日志结果用户 ERR

 用户:
    [
        {
            _id:5710b02c7787794009325f62,
            用户名:demouser
            __v:0,
            朋友:
            [
                {
                    用户名:janesmith
                    ID:7795239
                },
                {
                    用户名:JohnSmith对
                    ID:1363327
                }
            ]
        }
    ]
错误:空

这是关键的日志:

  toObject
的toJSON
项目
分贝

__v
ID
_ID
朋友
密码
用户名
用户帐号
架构
采集
comparePassword
保存
$ __三角洲
$ __版本
增量
$ __哪里
去掉
模型
$ __ buildDoc
在里面
$ __ storeShard

pre
岗位
删除pre
_lazySetupHooks
更新

$ __ shouldModify
$ __集
的getValue
setValue方法
得到
$ __路径
markModified
$ __尝试
modifiedPaths
将IsModified
isDirectModified
在里面
isSelected
验证
废止
$ __复位
$ __脏
$ __的setSchema
$ __ registerHooks
$ __错误
$ __ doQueue
$ toObject
检查
的toString
等于
填充
人口
$ __ FULLPATH

_events
_maxListeners
setMaxListeners
getMaxListeners
发射
的addListener

一旦
的removeListener
removeAllListeners
听众
listenerCount


解决方案

只需将您的结果变量初始化为数组,推动要匹配成数组并返回它的用户。

  User.find({}功能(ERR,用户){
    如果(ERR)res.send(ERR);    VAR的结果= [];
    对(在用户VAR键){
        若(网友[关键] .username == req.de coded.username){
            VAR的朋友=用户[关键] .friends;
            对于(VAR I = 0; I< friends.length;我++){
                //做的东西与每位朋友在这里,按要求。
            }            result.push(网友[关键]);            //或者这样,取决于它是否是用户还是你想回朋友。
            result.push(朋友[我]);
        }
    }    res.json(结果);
});

I have a api (in NODEJS) response that gives back an array:

res.json(friends)
[
    {
        "id": 7795239,
        "username": "janesmith"
    },
    {
        "id": 1363327,
        "username": "johnsmith"
    }
]

But when I want to iterate through the array before sending this response, I cannot find the way to do this. users.length does not exist, for(var key users) doens't work either.

How to solve this?

edit: the code where it gets the array:

User.find({}, function(err, users){
    if(err) res.send(err);
    var friends;
    var result = 'this will hold the result';

    for(var key in users){
        if(users[key].username == req.decoded.username) {
            friends = users[key].friends;
        }
    }

    // here I want to do things with the list of friends, so this is the place where I need to iterate over it

    res.json(result);
});

This is the log result for users and err

users: 
    [
        {
            "_id": "5710b02c7787794009325f62",
            "username": "demouser",
            "__v": 0,
            "friends": 
            [
                {
                    "username": "janesmith",
                    "id": 7795239
                },
                {
                    "username": "johnsmith",
                    "id": 1363327
                }
            ]
        }
    ]   
err: null

This is the log of the keys:

toObject
toJSON
items
db
discriminators
__v
id
_id
friends
password
username
userID
schema
collection
comparePassword
save
$__delta
$__version
increment
$__where
remove
model
$__buildDoc
init
$__storeShard
hook
pre
post
removePre
_lazySetupHooks
update
set
$__shouldModify
$__set
getValue
setValue
get
$__path
markModified
$__try
modifiedPaths
isModified
isDirectModified
isInit
isSelected
validate
invalidate
$__reset
$__dirty
$__setSchema
$__registerHooks
$__error
$__doQueue
$toObject
inspect
toString
equals
populate
populated
$__fullPath
domain
_events
_maxListeners
setMaxListeners
getMaxListeners
emit
addListener
on
once
removeListener
removeAllListeners
listeners
listenerCount

解决方案

Simply initialize your results variable as an array and push the users you wish to match into that array and return it.

User.find({}, function(err, users){
    if (err) res.send(err);

    var result = [];
    for (var key in users) {
        if (users[key].username == req.decoded.username) {
            var friends = users[key].friends;
            for (var i = 0; i < friends.length; i++) {
                // Do stuff with each friend here, as required.
            }

            result.push(users[key]);

            // OR this, depending on whether it is users or friends you're trying to return.
            result.push(friends[i]);
        }
    }

    res.json(result);
});

这篇关于无法通过JSON数组迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆