鑒于以下陣列(嵌套),我不確定如何使用 map 遍歷這些嵌套陣列以生成以下結果。
const qry = [
{
qryNm: "A",
qryResult: "",
qryWhere: [
{
qryReqs: [
{
col: "name",
op: "=",
colVal: "tom"
},
{
col: "country",
op: "=",
colVal: "germany"
}
]
},
{
qryReqs: [
{
col: "name",
op: "=",
colVal: "sally"
},
{
col: "country",
op: "=",
colVal: "italy"
}
]
}
]
},
{
qryNm: "B",
qryResult: "",
qryWhere: [
{
qryReqs: [
{
col: "name",
op: "=",
colVal: "kirk"
},
{
col: "country",
op: "=",
colVal: "sweeden"
}
]
},
{
qryReqs: [
{
col: "name",
op: "=",
colVal: "grace"
},
{
col: "country",
op: "=",
colVal: "usa"
}
]
}
]
}
]
我需要遍歷上面的陣列并產生以下結果:
(name = 'tom' and
country = 'germany')
or
(name = 'sally' and
country = 'italy')
并將此值分配給qryResultqry 陣列內,為此qryNm,即:
qryResult = "(name = 'tom' and country = 'germany') or (name = 'sally' and country = 'italy')";
顯然,這qryResult需要對陣列中的每個陣列元素進行處理qry。
因此,對于qryReqsarray 中的每個,將其qryWhere分解為“OR”條件。
擁有三層陣列,我不確定如何實作我的結果。
我意識到我需要使用:
qry.map((q, i) => (
// unsure from here
))
任何幫助/指導都會很棒。
uj5u.com熱心網友回復:
您可以將問題分解為多個部分。首先,您需要轉動以下物件 ( req):
{col: "name", op: "=", colVal: "sally"}
進入字串:
name = 'sally'
這可以通過使用模板文字(或字串連接)和訪問物件中的col, op和colVal屬性來完成:
`${req.col} ${req.op} '${req.colVal}'`
現在您可以將單個物件轉換為字串,您可以qryReqs使用上面的模板文字映射最里面的 ( ) 陣列:
qryReqs: [
{ col: "name", op: "=", colVal: "sally" },
{ col: "country", op: "=", colVal: "italy" }
]
這個映射函式看起來像:
qryReqs.map(req => `${req.col} ${req.op} '${req.colVal}'`)
這會產生一個字串陣列:
["name = 'sally'", "country = 'italy'"]
在您的輸出中,您希望它采用以下形式:(string[0] and string[1] and string[2] and ... and string[n])。我們可以使用連接這個陣列中.join(' and ')的字串來得到一個字串,然后將該字串包裹在括號中():
`(${qryReqs.map(req => `${req.col} ${req.op} '${req.colVal}'`).join(' and ')})`
這樣做為我們提供了一個用“and”連接的字串,用于括號中的每個內部物件:
"(name = 'sally' and country = 'italy')"
由于我們要對 內的所有物件執行上述映射操作qryWhere,我們可以qryWhere將上述模板字面量 join() 邏輯映射并應用到每個物件:
qryWhere.map(
({qryReqs}) => `(${qryReqs.map(req => `${req.col} ${req.op} '${req.colVal}'`).join(' and ')})`
)
做以上給我們:
["(name = 'tom' and country = 'germany')", "(name = 'sally' and country = 'italy')"]
有了這個結果,我們需要用“或”子句將它們連接在一起以匹配您的輸出,這可以通過將上述結果陣列與.join(" or ")以下內容連接來完成:
qryWhere.map(
({qryReqs}) => `(${qryReqs.map(req => `${req.col} ${req.op} '${req.colVal}'`).join(' and ')})`
).join(' or ')
You can then perform an outer .map() on your qry array to perform this conversion for all objects in your array:
const qry = [{ qryNm: "A", qryWhere: [{ qryReqs: [{ col: "name", op: "=", colVal: "tom" }, { col: "country", op: "=", colVal: "germany" } ] }, { qryReqs: [{ col: "name", op: "=", colVal: "sally" }, { col: "country", op: "=", colVal: "italy" } ] } ] }];
const results = qry.map(({qryWhere}) => qryWhere.map(
({qryReqs}) => `(${qryReqs.map(req => `${req.col} ${req.op} '${req.colVal}'`).join(' and ')})`
).join(' or '));
const [res] = results; // if you want to extract the first result
console.log(results);
console.log(res);
If you want to add an additional property qryResult to your objects, this can be done by creating a new object {} in your map callback, and then using the spread syntax ... to copy all the (enumerable own) keys from the current object to the new object. You can then add the new qryResult to the new object with the value of the above mapping:
const qry = [{ qryNm: "A", qryWhere: [{ qryReqs: [{ col: "name", op: "=", colVal: "tom" }, { col: "country", op: "=", colVal: "germany" } ] }, { qryReqs: [{ col: "name", op: "=", colVal: "sally" }, { col: "country", op: "=", colVal: "italy" } ] } ] }];
const result = qry.map((qryObj) => ({
...qryObj,
qryResult: qryObj.qryWhere.map(
({qryReqs}) => `(${qryReqs.map(req => `${req.col} ${req.op} '${req.colVal}'`).join(' and ')})`
).join(' or ')
}));
console.log(result);
console.log(result[0].qryResult);
uj5u.com熱心網友回復:
這是實作類似結果的一種方法
const qry = [{
qryNm: 'A',
qryWhere: [{
qryReqs: [{
col: 'name',
op: '=',
colVal: 'tom',
},
{
col: 'country',
op: '=',
colVal: 'germany',
},
],
},
{
qryReqs: [{
col: 'name',
op: '=',
colVal: 'sally',
},
{
col: 'country',
op: '=',
colVal: 'italy',
},
],
},
],
}, ]
const result = qry
.map(obj1 =>
obj1.qryWhere
.map(
obj2 =>
`( name = '${obj2.qryReqs[0].colVal}' and country = '${obj2.qryReqs[1].colVal}' )`
)
.join(' or ')
)
.join(' or ')
console.log(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355600.html
標籤:javascript 数组 目的 嵌套
上一篇:Rust中的閉包陣列
下一篇:從字串陣列中洗掉空格的問題
