在JavaScript中,你将如何构建一个价值与B值进行比较的方法 [英] In javascript, how would you build a method that compares value A with value B

查看:90
本文介绍了在JavaScript中,你将如何构建一个价值与B值进行比较的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的数组,像这样:

I have an array of objects, something like this:

var myArray = [
   { 'name' : 'some name', id: 23131, 'has' : ['dogs'] },
   { 'name' : 'some name 2', id: 8678, 'has' : ['dogs', 'cats'] },
   { 'name' : 'some name 3', id: 2125 , 'has' : ['donkeys', 'goats']},
   { 'name' : 'some name 4', id: 90867, 'has' : ['parrots', 'treasure'] },
   { 'name' : 'some name 5', id: 435458, 'has' : undefined },
];

我想检索符合特定条件的具体内容。例如,一个人的姓名中含有数字5,id是435458.或者谁拥有一只鹦鹉或山羊,或者一个人两个。

And I want to retrieve specific elements that match certain criteria. For example, a person whose name contains number 5, and id is 435458. Or a person who has a parrot or a goat, or both.

我试图构建的方法有两个参数,值A和值B值A的对象,如 {'名':'5'} {'名':/ 5 /} {'名':5} 或{'有': '山羊'}和B值来匹配,即 myArray的的对象。

The method I'm trying to build takes two arguments, value A and value B. Value A is an object, like { 'name' : '5' } or { 'name' : /5/ } or { 'name' : 5 }or { 'has' : 'goats' }, and value B is the object to match against, i.e. myArray.

该方法正迅速成为相当复杂的,我觉得我的code是不是很有效和高效的,因为它可以。

The method is quickly becoming quite complex and I feel that my code is not quite as effective and efficient as it could.

我认为实现这一目标的最佳方式是通过对象和数组的传递和发现( myArray的数组),并调用它的自我,直到有只有两个字符串/数字/正则表达式值对进行比较。但我不如何最好地实现这一目标很有把握。或者这是不是最好的方法去?此外,速度是一个重要的成功标准。

I think the best way to achieve this is to loop through the objects and arrays that are passed and found (myArray, has array), and call it self until there only two string/number/regexp values to be compared against. But I'm not quite sure on how to best achieve this. Or is this not the best way to go? Also, speed is an important success criterion.

干杯

编辑: http://jsbin.com/ediye/edit 包含了我使用的功能现在,我觉得它的工作原理如上所述,但其庞大的数据集相当缓慢。

http://jsbin.com/ediye/edit contains the function I'm using now, and I think it works as described above, but its quite slow for large data sets.

推荐答案

好吧,我看到相当多的人有兴趣在此所以这里的我有什么到现在:

Ok, I'm seeing that quite a few people are interested in this so here's what I have up to now:

var filter = function(what,where) {
    var sameArrays = function(arr,arr1) {
    	var al = arr.length, bl = arr1.length;
    	if (al !== bl || al === 0 || bl === 0) { return; }
    	for (var i = al - 1; i >= 0; i--){
    		if (!match(arr[i], arr1[i])) { return; }
    	}
    	return arr1;
    };

    var sameObjects = function(obj,obj1) {
    	for (var i in obj) {
    		if(!obj1.hasOwnProperty(i)){return;}
    		if (!match(obj[i],obj1[i])) {return;}
    	}
    	return obj1;
    };

    var inArray = function(value,obj) {
    	var r = [],
    		m = match;

    	for (var i = obj.length - 1; i >= 0; i--){
    		var item = obj[i];
    		if( m(value,item) ) {
    			r.push( item );
    		}
    	};

    	return (r.length) ? r : undefined ;
    };

    var inObject = function(value,obj) {
    	var r = [],
    		m = match;

    	for(var i in obj){
    		var item = obj[i];
    		if( m(value,item) ) {
    			r.push( item );
    		}
    	};

    	return (r.length) ? r : undefined ;
    };

    var inString = function(value,string) {	
    	var	valueConstructor = value.constructor;

    	if( valueConstructor === RegExp) {
    		return (value.test(string)) ? string : undefined ; }

    	else if( valueConstructor === Function && value(string) === true ) {
    		return string; }

    };

    var match = function(a,b) {
    	if(typeof a === 'undefined' || typeof b === 'undefined' || a === null || b === null) {
    		return; }

    	if (a == b) { 
    		return b;}

    	var	vc = a.constructor,
    		oc = b.constructor;

    // Cannot compare array or object to a string or a number
    	if( (vc === Array || vc === Object) && (oc === String || oc === Number) ) {
    		return;	}

    	if( oc === Array && vc === Array ) {
    		return sameArrays(a,b);	}
    	else if(oc === Array) {
    		return inArray(a,b); }
    	else if (oc === Object && vc === Object) {
    		return sameObjects(a,b); }
    	else if (oc === Object) {
    		return inObject(a,b); }			
    	else if (vc === Object || vc === Array) {
    		return;	}
    	else if (oc === String || oc === Number){
    		return inString(a,b); }

    };

    return match(what,where);
};

允许你做到以下几点:

Which allows you do the following:

var b = [
   { 'name' : 'some name', age: 23, id: 0, 'has' : ['dogs'] },
   { 'name' : 'some name 2', age:24, id: 1, 'has' : ['dogs', 'cats'] },
   { 'name' : 'some name 3', age: 25, id: 2 , 'has' : ['donkeys', 'goats']},
   { 'name' : 'some name 4', age:26, id: 3, 'has' : ['parrots', 'treasure'] },
   { 'name' : 'some name 5', age:27, id: 4, 'has' : undefined }
];

filter({ age:23 },b)

filter({ age:'24' },b)

filter({ name: /\d$/ },b)

filter('dogs',b)

filter({ has: 'goats' },b)

这是一些JS库的,我一直在努力的一部分,所以如果你发现使用JSON + DOM的尝试搜索谷歌code对于 JDataStore的

This is part of some sort of js library that I've been working on, so if you find working with json + dom try searching google code for jdatastore.

这篇关于在JavaScript中,你将如何构建一个价值与B值进行比较的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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