数组元素和对象属性的比较返回false

时间:2015-11-05 23:25:43

标签: javascript arrays object compare

我想输出哪些数组元素等于对象属性。

Array和Object就是这样:

var groupProperties = {
    "processors": ["producent: ","model-line: ","model: ","freq: ","cores: ","threads: ","quantity: ","netto_price: "],

    "graphics": ["producent: ","model_line: ","model: ","vram: ","directx: ","quantity: ","netto_price: "],

    "motherboards": ["producent: ","model_line: ","model: ","chipset: ","quantity: ","netto_price: "]
};

var groups = [
    PROCESSORS
    ,AMD|ATHLON|7750|2.6|6|2|7|250.00
    INTEL|i7|720QM|2.8|4|8|5|450.25
    INTEL|i5|4570|3.2|4|4|4|345.50
    ,GRAPHICS
    ,AMD|RADEON|7790|1GB|11|3|300.90
    NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99
    ,MOTHERBOARDS
    ,MSI|GAMING|GD-65|Z87|5|510.30
    ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99
    ASUS|UNKNOWN|P5KC|P35|1|50.00
];

我的功能不起作用(如果声明返回false,则不知道原因):

for (var index in groups) {
    for(var property in groupProperties) {
        console.log('PROPS '+property);
        console.log("second loop \n");
        console.log("ARRAY "+groups[index]);
        if(groups[index] === property.toUpperCase()) { 
             console.log("third loop \n");
             console.log("Array elements equal with object properties "+groups[index]);
        }
    }            
}

输出应该是:'PROCESSORS','GRAPHICS','MOTHERBOARDS'

-------------------------------------------- -----------------------------------------

@edit 我发现 groups 数组有问题,因为在控制台中看起来像这样:http://imgur.com/FH7Geu8

所以我给整个代码操作所有原始数组。我希望有人帮助修复它。:

// var content <= there is text loaded from '.txt' file

            var splittedRows= content.split('\n');
            console.log("Splitted: \n"+ splittedRows);

            var groups = [];
            var inner = 0;

            for (index in splittedRows)
            {
                if (splittedRows[index].indexOf('GRP') == 0)
                {
                    groups[index] = splittedRows[index].slice('PRD|'.length);
                    inner = Number(index) + 1;
                        console.log('inner '+inner);
                    continue;
                }
                if (groups[inner] === undefined)
                {
                    groups[inner] = splittedRows[index].slice('PRD|'.length);
                    continue;
                }
                groups[inner] += splittedRows[index].slice('PRD|'.length);
            }
            console.log(groups);

            for(i=0; i<groups.length; i++)
            {

                if (!groups[i])
                {
                    groups.splice(i,1);
                    i--;
                }
                    //console.log("Moved "+groups[i]);
            }

也是来自文本加载位置的来源:

  

GRP |处理器PRD | AMD | ATHLON | 7750 | 2.6 | 6 | 2 | 7 | 250.00   PRD | INTEL | i7 | 720QM | 2.8 | 4 | 8 | 5 | 450.25 PRD | INTEL | i5 | 4570 | 3.2 | 4 | 4 | 4 | 345.50   GRP | GRAPHICS PRD | AMD | RADEON | 7790 | 1GB | 11 | 3 | 300.90   珠三角| NVIDIA | GEFORCE | 8800GTS | 512MB | 10 | 1 | 150.99 GRP |主板   珠三角|微星|游戏| GD-65 | Z87 | 5 | 510.30   珠三角|华硕| RAMPAGE | MAXIMUS-VII | Z87 | 6 | 890.99   PRD | ASUS | UNKNOWN | P5KC | P35 | 1 | 50.00

@ EDIT2

我通过拼接 undefined元素修复数组,并从数组中切片'\ r'字符输出元素。 现在一切正常 - 我可以将数组元素与对象的属性进行比较。

2 个答案:

答案 0 :(得分:1)

您可以使用Array.prototype.filterObject.prototype.hasOwnProperty轻松完成此操作:

var groupProperties = {
    "processors": ["producent: ","model-line: ","model: ","freq: ","cores: ","threads: ","quantity: ","netto_price: "],

    "graphics": ["producent: ","model_line: ","model: ","vram: ","directx: ","quantity: ","netto_price: "],

    "motherboards": ["producent: ","model_line: ","model: ","chipset: ","quantity: ","netto_price: "]
};

var groups = [
    "PROCESSORS",
    "AMD|ATHLON|7750|2.6|6|2|7|250.00",
    "INTEL|i7|720QM|2.8|4|8|5|450.25",
    "INTEL|i5|4570|3.2|4|4|4|345.50",
    "GRAPHICS",
    "AMD|RADEON|7790|1GB|11|3|300.90",
    "NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99",
    "MOTHERBOARDS",
    "MSI|GAMING|GD-65|Z87|5|510.30",
    "ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99",
    "ASUS|UNKNOWN|P5KC|P35|1|50.00"
];

var propGroups = groups.filter(function(group) {
    return groupProperties.hasOwnProperty(group.toLowerCase());
});

console.log(propGroups);

答案 1 :(得分:0)

您需要将这些数组条目包装在引号中,如下所示:

var groups = [
  "PROCESSORS",
  "AMD|ATHLON|7750|2.6|6|2|7|250.00",
  "INTEL|i7|720QM|2.8|4|8|5|450.25",
  "INTEL|i5|4570|3.2|4|4|4|345.50",
  "GRAPHICS",
  "AMD|RADEON|7790|1GB|11|3|300.90",
  "NVIDIA|GEFORCE|8800GTS|512MB|10|1|150.99",
  "MOTHERBOARDS",
  "MSI|GAMING|GD-65|Z87|5|510.30",
  "ASUS|RAMPAGE|MAXIMUS-VII|Z87|6|890.99",
  "ASUS|UNKNOWN|P5KC|P35|1|50.00"
];