如何在 javascript 中连接这种类型的 JSON?
How can I concatenate this type of JSON in javascript?
如何连接此 json 以获得它:
complements = ["XYZ 3, CDE TR, AAA 5", "", "NDP 3, DDD FR"]
?
每个地址都可以包含一组补码,这些补码必须连接起来并用逗号分隔。
P.s:我正在使用 javascript。
P.s2:补语可以为空,就像 JSON 中的第二组一样。
[
{
"postalcode": "1234",
"street": "ABC",
"number": "1",
"complement": [
{
"type": "B",
"name": "XYZ",
"description": "3"
},
{
"type": "C",
"name": "CDE",
"description": "TR"
},
{
"type": "D",
"name": "AAA",
"description": "5"
}
]
},
{
"postalcode": "444",
"street": "No complements",
"number": "5"
},
{
"postalcode": "2222",
"street": "BBB",
"number": "2",
"complement": [
{
"type": "E",
"name": "NDP",
"description": "3"
},
{
"type": "F",
"name": "DDD",
"description": "FR"
}
]
}
];
我的代码 我得到 this.complementsList.forEach 不是函数.
getComplement(addressesResponse){
this.complementsList = JSON.parse(addressesResponse);
this.complementsList.forEach((item) => {
Object.defineProperty(item, 'complements', {
get: function() {
return this.complement.map((c) => `${c.name} ${c.description}`).join(', '); }
})
});
来源:https://salesforce.stackexchange.com/questions/367713/how-to-render-a-json-in-the-same-line-lwc
有了一个 javascript 对象,您可以遍历对象的键并将其中一些组合成字符串
它将看起来像这样:
const jsonObject = [{...}, {...}, ...]
const complements = [];
jsonObject.forEach((item) => {
let complement = item['complement'].reduce((result, currObj)
=> result += (currObj.name+' '+currObj.description), "");
complements.push(complement);
});
这只是一个例子。有很多方法可以做到。
我是怎么解决的:
arr.map((x)=>x.complement != null? (x.complement.map((y)=>y.name+' '+y.description)+"") :'');
如何连接此 json 以获得它:
complements = ["XYZ 3, CDE TR, AAA 5", "", "NDP 3, DDD FR"]
?
每个地址都可以包含一组补码,这些补码必须连接起来并用逗号分隔。
P.s:我正在使用 javascript。 P.s2:补语可以为空,就像 JSON 中的第二组一样。
[
{
"postalcode": "1234",
"street": "ABC",
"number": "1",
"complement": [
{
"type": "B",
"name": "XYZ",
"description": "3"
},
{
"type": "C",
"name": "CDE",
"description": "TR"
},
{
"type": "D",
"name": "AAA",
"description": "5"
}
]
},
{
"postalcode": "444",
"street": "No complements",
"number": "5"
},
{
"postalcode": "2222",
"street": "BBB",
"number": "2",
"complement": [
{
"type": "E",
"name": "NDP",
"description": "3"
},
{
"type": "F",
"name": "DDD",
"description": "FR"
}
]
}
];
我的代码 我得到 this.complementsList.forEach 不是函数.
getComplement(addressesResponse){
this.complementsList = JSON.parse(addressesResponse);
this.complementsList.forEach((item) => {
Object.defineProperty(item, 'complements', {
get: function() {
return this.complement.map((c) => `${c.name} ${c.description}`).join(', '); }
})
});
来源:https://salesforce.stackexchange.com/questions/367713/how-to-render-a-json-in-the-same-line-lwc
有了一个 javascript 对象,您可以遍历对象的键并将其中一些组合成字符串
它将看起来像这样:
const jsonObject = [{...}, {...}, ...]
const complements = [];
jsonObject.forEach((item) => {
let complement = item['complement'].reduce((result, currObj)
=> result += (currObj.name+' '+currObj.description), "");
complements.push(complement);
});
这只是一个例子。有很多方法可以做到。
我是怎么解决的:
arr.map((x)=>x.complement != null? (x.complement.map((y)=>y.name+' '+y.description)+"") :'');