对象 es6 的条件解构数组
Conditional destructuring array of objects es6
堆栈闪电战:demo
思路是服务器发送如下格式的响应,需要根据以下条件决定在页面上显示或隐藏按钮,每个按钮都有单独的点击功能。这就是我在页面中静态声明按钮的方式。
我下面有一组对象。我需要在某些条件下将对象的属性映射到其他属性。
collections = [
{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
},
]
这是对象的集合数组。
这里我尝试根据两个条件来映射对象的属性,
如果 productId
值匹配 'string'
和 isAvailable
属性 是 true
我已经分配给全局变量并显示按钮。但它是错误的。任何人帮助代码我做错了。
getClick() {
let showButtonSamsung, showButtonNokia, showButtonLg;
let x = this.collections.map(x => {
showButtonSamsung = x.productId == 'samsung' && x.isAvailable == true ? true : false;
showButtonNokia = x.productId =='nokia' && x.isAvailable == true ? true : false;
showButtonLg = x.productId == 'Lg' && x.isAvailable == true ? true : false;
});
}
预计O/P:
showButtonSamsung: true // will show the button
showButtonNokia: true // will show the button
showButtonLg: false // hide the button
我认为在这种情况下 reduce
会好得多。
let collections = [{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
}
]
const map = {
samsung: "showButtonSamsung",
nokia: "showButtonNokia",
Lg: "showButtonLg"
}
const {showButtonSamsung, showButtonNokia, showButtonLg} = collections.reduce((acc, obj) => {
const property = map[obj.productId];
acc[property] = obj.isAvailable;
return acc;
}, {})
console.log(showButtonSamsung, showButtonNokia, showButtonLg);
我认为这或多或少是您要找的:
const collections = [
{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
}];
let isAvailable = (brand, collections) => collections.some((x) => x.productId === brand && x.isAvailable)
let x = {
showButtonSamsung: isAvailable('samsung', collections),
showButtonNokia: isAvailable('nokia', collections),
showButtonLg: isAvailable('Lg', collections),
}
console.log(x);
另一种选择:
let x = {
showButtonSamsung: 'samsung',
showButtonNokia: 'nokia',
showButtonLg: 'Lg',
}
let isAvailable = (brand, collections) => collections.some((x) => x.productId === brand && x.isAvailable)
x = Object.entries(x).map(([key, value]) => ([key, isAvailable(value, collections)]))
.reduce((obj, arr) => ({
...obj, [arr[0]]: arr[1]
}), {})
console.log(x);
堆栈闪电战:demo
思路是服务器发送如下格式的响应,需要根据以下条件决定在页面上显示或隐藏按钮,每个按钮都有单独的点击功能。这就是我在页面中静态声明按钮的方式。
我下面有一组对象。我需要在某些条件下将对象的属性映射到其他属性。
collections = [
{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
},
]
这是对象的集合数组。 这里我尝试根据两个条件来映射对象的属性,
如果 productId
值匹配 'string'
和 isAvailable
属性 是 true
我已经分配给全局变量并显示按钮。但它是错误的。任何人帮助代码我做错了。
getClick() {
let showButtonSamsung, showButtonNokia, showButtonLg;
let x = this.collections.map(x => {
showButtonSamsung = x.productId == 'samsung' && x.isAvailable == true ? true : false;
showButtonNokia = x.productId =='nokia' && x.isAvailable == true ? true : false;
showButtonLg = x.productId == 'Lg' && x.isAvailable == true ? true : false;
});
}
预计O/P:
showButtonSamsung: true // will show the button
showButtonNokia: true // will show the button
showButtonLg: false // hide the button
我认为在这种情况下 reduce
会好得多。
let collections = [{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
}
]
const map = {
samsung: "showButtonSamsung",
nokia: "showButtonNokia",
Lg: "showButtonLg"
}
const {showButtonSamsung, showButtonNokia, showButtonLg} = collections.reduce((acc, obj) => {
const property = map[obj.productId];
acc[property] = obj.isAvailable;
return acc;
}, {})
console.log(showButtonSamsung, showButtonNokia, showButtonLg);
我认为这或多或少是您要找的:
const collections = [
{
"productId": "samsung",
"productParams": "",
"isAvailable": true
},
{
"productId": "nokia",
"productParams": "",
"isAvailable": true
},
{
"productId": "Lg",
"productParams": "",
"isAvailable": false
}];
let isAvailable = (brand, collections) => collections.some((x) => x.productId === brand && x.isAvailable)
let x = {
showButtonSamsung: isAvailable('samsung', collections),
showButtonNokia: isAvailable('nokia', collections),
showButtonLg: isAvailable('Lg', collections),
}
console.log(x);
另一种选择:
let x = {
showButtonSamsung: 'samsung',
showButtonNokia: 'nokia',
showButtonLg: 'Lg',
}
let isAvailable = (brand, collections) => collections.some((x) => x.productId === brand && x.isAvailable)
x = Object.entries(x).map(([key, value]) => ([key, isAvailable(value, collections)]))
.reduce((obj, arr) => ({
...obj, [arr[0]]: arr[1]
}), {})
console.log(x);