Javascript `lookupIndex[row[lookupKey]]=row;`工作
我在阅读时遇到了这个不熟悉的JavaScript语法。语法如下:Javascript `lookupIndex[row[lookupKey]]=row;`工作,javascript,Javascript,我在阅读时遇到了这个不熟悉的JavaScript语法。语法如下: lookupIndex[row[lookupKey]]=row 有人知道这里发生了什么吗?我没见过这样的语法。在上下文中使用: 数据 var articles = [{ "id": 1, "name": "vacuum cleaner", "weight": 9.9, "price": 89.9, "brand_id": 2 }, { "id": 2, "name": "wa
lookupIndex[row[lookupKey]]=row代码>
有人知道这里发生了什么吗?我没见过这样的语法。在上下文中使用:
数据
var articles = [{
"id": 1,
"name": "vacuum cleaner",
"weight": 9.9,
"price": 89.9,
"brand_id": 2
}, {
"id": 2,
"name": "washing machine",
"weight": 540,
"price": 230,
"brand_id": 1
}, {
"id": 3,
"name": "hair dryer",
"weight": 1.2,
"price": 24.99,
"brand_id": 2
}, {
"id": 4,
"name": "super fast laptop",
"weight": 400,
"price": 899.9,
"brand_id": 3
}];
var brands = [{
"id": 1,
"name": "SuperKitchen"
}, {
"id": 2,
"name": "HomeSweetHome"
}];
函数和调用
function join(lookupTable, mainTable, lookupKey, mainKey, select) {
var l = lookupTable.length,
m = mainTable.length,
lookupIndex = [],
output = [];
for (var i = 0; i < l; i++) { // loop through l items
var row = lookupTable[i];
lookupIndex[row[lookupKey]] = row; // create an index for lookup table
}
for (var j = 0; j < m; j++) { // loop through m items
var y = mainTable[j];
var x = lookupIndex[y[mainKey]]; // get corresponding row from lookupTable
output.push(select(y, x)); // select only the columns you need
}
return output;
};
var result = join(brands, articles, "id", "brand_id", function(article, brand) {
return {
id: article.id,
name: article.name,
weight: article.weight,
price: article.price,
brand: (brand !== undefined) ? brand.name : null
};
});
console.log(result);
函数联接(lookupTable、mainTable、lookupKey、mainKey、select){
var l=可查找的.length,
m=主表长度,
lookupIndex=[],
输出=[];
对于(var i=0;i
感谢您的回答或指点,谢谢 将其视为两个独立的函数调用:
var rowLookup = row[lookupKey];
lookupIndex[rowLookup] = row;
这与在同一条线上做这一切是一样的:
lookupIndex[row[lookupKey]] = row;
将其视为两个独立的函数调用:
var rowLookup = row[lookupKey];
lookupIndex[rowLookup] = row;
这与在同一条线上做这一切是一样的:
lookupIndex[row[lookupKey]] = row;
谢谢@freedomn-m。但是,控制台输出是不同的。使用点表示法为keybrand
@freedomn-m生成一个null
值不,这不是一回事-您编写的内容与lookupIndex[row[“lookupKey”]]
(带引号)。谢谢@freedomn-m。但是,控制台输出是不同的。使用点表示法为keybrand
@freedomn-m生成一个null
值不,这不是一回事-您编写的内容与lookupIndex[row[“lookupKey”]]
(带引号)相同。细分是正确的,只是您应该调用它表达式,而不是函数调用。因此在您的重新措辞lookupIndex[rowLookup]=行代码>指定var row=lookupTable[i];//品牌[i]
和var rowLookup=row[lookupKey]代码>到查找索引
?有点像是推送的工作?思想,@PeterB?分解是正确的,除了你应该调用表达式而不是函数调用代码>指定var row=lookupTable[i];//品牌[i]
和var rowLookup=row[lookupKey]代码>到查找索引
?有点像是推送的工作?思想,@PeterB?