查询到一个数组的JavaScript

查询到一个数组的JavaScript

本文介绍了查询到一个数组的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能做一个查询的JavaScript,比如我想找到以A开头,一个则显示所有信息全部名称的数组

名称:伟业,时代:14结果
名称:艾莉森,时代:14

这是我的数组JSON:

  VAR房子= [{名:杰森,时代:43},
{姓名:伟业,时代:14},
{名:运气,时代:14},
{姓名:艾莉森,时代:14},
{名:汤姆,时代:12}]


解决方案

您可以过滤数组,收集要素与'A'的名字,然后的forEach或比赛映射到属性名称和值的新数组

  house.filter(功能(ITM){
    返回itm.Name.charAt(0)=='A';
})地图(功能(O){
    VAR A = [];
    对(邻变种P){
        如果(o.hasOwnProperty(P)){
            A.push(P +:+ O [P]);
        }
    }
    回归A;
})加入('\\ n');

/ *返回值:(字符串)* /

 名称:伟业,年龄:14
名称:艾莉森,年龄:14

如果你需要一个垫片:

 (函数(){
    VAR A = Array.prototype;
    如果(!A.filter)A.​​filter =功能(好玩,范围){
        VAR T =此,A = [],I = 0,ITM,L = T.length;
        如果(typeof运算的乐趣=='功能'){
            而(ⅰ&下:L){
                如果(我T){
                    ITM = T [I]
                    如果(fun.call(范围,ITM,I,T))A [则为a.length] = ITM;
                }
                ++我;
            }
        }
        回归A;
    }
    如果(!A.map)A.map =功能(好玩,范围){
        VAR T =此,L = T.length,A =阵列(L),I = 0;
        如果(typeof运算的乐趣=='功能'){
            而(ⅰ&下:L){
                如果(我T){
                    A [i] = fun.call(范围,T [I],I,T);
                }
                ++我;
            }
            回归A;
        }
    }
}
})();

How can I do a query to an array javascript, for example I want to find all names that begin with A, an then show all information

Name":"Albert","age":"14"
Name":"Alison","age":"14"

this is my array json:

var house = [{"Name":"Jason","age":"43"},
{"Name":"Albert","age":"14"},
{"Name":"Luck","age":"14"},
{"Name":"Alison","age":"14"},
{"Name":"Tom","age":"12"}]
解决方案

You can filter an array, to collect the elements with an 'A' name, then forEach or map the matches to a new array of property names and values

house.filter(function(itm){
    return itm.Name.charAt(0)== 'A';
}).map(function(o){
    var A= [];
    for(var p in o){
        if(o.hasOwnProperty(p)){
            A.push(p+':'+o[p]);
        }
    }
    return A;
}).join('\n');

/* returned value: (String)*/

Name:Albert,age:14
Name:Alison,age:14

If you need a shim:

(function(){
    var A= Array.prototype;
    if(!A.filter)   A.filter= function(fun, scope){
        var T= this, A= [], i= 0, itm, L= T.length;
        if(typeof fun== 'function'){
            while(i<L){
                if(i in T){
                    itm= T[i];
                    if(fun.call(scope, itm, i, T)) A[A.length]= itm;
                }
                ++i;
            }
        }
        return A;
    }
    if(!A.map) A.map= function(fun, scope){
        var T= this, L= T.length, A= Array(L), i= 0;
        if(typeof fun== 'function'){
            while(i<L){
                if(i in T){
                    A[i]= fun.call(scope, T[i], i, T);
                }
                ++i;
            }
            return A;
        }
    }
}
})();

这篇关于查询到一个数组的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 19:23