返回json对象

时间:2011-08-26 23:37:50

标签: javascript jquery json

我有一组像

这样的对象
 objArray =   [{"color":"red", "width":5, "price":10},
    {"color":"green", "width":4, "price":8},
    {"color":"red", "width":7, "price":11}]

如何在javascript或jquery

中返回颜色为红色的对象数组

3 个答案:

答案 0 :(得分:3)

 var objArray =   [{"color":"red", "width":5, "price":10},
    {"color":"green", "width":4, "price":8},
    {"color":"red", "width":7, "price":11}]

 var tmp = []

 $.each(objArray,function(i,val){
     if(val.color == 'red')
     {
         tmp.push(objArray[i]);
     }
 });

 console.log(tmp);

答案 1 :(得分:3)

如果通过“return”,表示从函数返回,那么您可以像这样使用Array.prototype.filter

return objArray.filter(function(v) { return v.color === 'red'; });

如果您只想将其保存到变量中,那么它就差不多了:

var red_array = objArray.filter(function(v) { return v.color === 'red'; });

要覆盖旧浏览器,请使用the MDN .filter() shim

if (!Array.prototype.filter) {
    Array.prototype.filter = function (fun /*, thisp */ ) {
        "use strict";

        if (this === void 0 || this === null) throw new TypeError();

        var t = Object(this);
        var len = t.length >>> 0;
        if (typeof fun !== "function") throw new TypeError();

        var res = [];
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in t) {
                var val = t[i]; // in case fun mutates this
                if (fun.call(thisp, val, i, t)) res.push(val);
            }
        }

        return res;
    };
}

或者如果你想要jQuery,请使用$.map()[docs]方法:

var arr = $.map( objArray, function(v) { if( v.color === 'red' ) return v; });

$.grep()[docs]方法:

var arr = $.grep( objArray, function(v) { return v.color === 'red'; });

示例: http://jsbin.com/udawir/edit#javascript,live (更改传递给$.each()的数组以记录生成的颜色值)

答案 2 :(得分:0)

简短

server {
 listen 80;
 listen [::]:80;
 location / {
   root /var/www/mountainmanor/build;
   index index.html index.htm index.nginx-debian.html;
   try_files $uri /index.html;
        }
 server_name xxxxxxxx;
 return 301 $scheme://mountainmanorskillednursing.com$request_uri;
location /api/ {
    proxy_pass http://xxxxxxx:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
 }
}
server {
 location / {
   root /var/www/mountainmanor/build;
   index index.html index.htm index.nginx-debian.html;
   try_files $uri /index.html;
        }
    server_name mountainmanorskillednursing.com www.mountainmanorskillednursing.com; # managed by Certbot
location /api/ {
    proxy_pass http://xxxxxxx:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
 }


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mountainmanorskillednursing.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



}server {
    if ($host = www.mountainmanorskillednursing.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = mountainmanorskillednursing.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


 listen 80 ;
 listen [::]:80 ;
    server_name mountainmanorskillednursing.com www.mountainmanorskillednursing.com;
}

希望有帮助!