JS个
let data1 = [{'id' : '0001', 'area' : 'Paris', 'price' : '100', 'year': '2022'}]
//should construct condition if(id === '0001' && area === 'Paris' && price === '100 && year === '2022')
let data2 = [{'price' : '300', 'year': '2021'}]
//should construct condition if(price === '300' && year === '300')
let data3 = [{'area' : 'Athens'}]
//should construct condition if(area === 'Athens')
// no && when we have only one condition
//my code attempt fo far
let ArrayNew = [];
let operator_and = ' && ';
for(let i = 0; i < data1.length; i++) {
ArrayNew += data1.map(x => Object.keys(x) + ' === ' + operator_and);
}
console.log('if ('+ ArrayNew +')'); // if (id,area,price,year === && )
Question个
我试图在if()语句中动态构建条件,不仅要根据键的数量,还要根据每个给定数组的键.
我希望能创造出这样的东西
if (generated_conditions) { .... }
个