我想输出哪些数组元素等于对象属性。数组元素和对象属性的比较返回false
数组和对象是这样的:
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
];
我的功能,这是不行的(if语句返回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]);
}
}
}
输出应成为:'处理器','图形','主板'。
--------------------------------------------- ----------------------------------------
@edit 我发现的东西是错误的组阵列,原因在控制台,它看起来是这样的: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 | PROCESSORS珠三角| 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 | | | 3 | 300.90 PRD | NVIDIA | GEFORCE | 8800GTS | 512MB | 10 | 1 | 150.99 GRP |主板 PRD | MSI | GAMING | GD-65 | Z87 | 5 | 510.30 PRD |华硕| RAMPAGE | MAXIMUS- VII | Z87 | 6 | 890.99 PRD |华硕| UNKNOWN | P5KC | P 35 | 1 | 50.00
@ EDIT2
我通过splice'ingundefined
元件和slice'ing '\ R' 从数组元素煤焦出固定组阵列。 现在所有的作品 - 我可以比较数组元素与对象的属性。
你的群体缺失引号。 – SimpleJ
是你使用的代码??!?!?那不是JS。 – Amit
@SimpleJ,groups元素是String类型的,我刚刚从控制台粘贴了数组输出。 **阿米特**,你是什么意思?除了在内部循环中的所有作品。如果我设置**!== **,那么一些元素出现,但不是我想要的那些 – Chris92