admin管理员组文章数量:1026989
I have const tab where I want to change property isActive from boolean to 'on' or 'off' depending on if it's true or false.
const tab = [
{id:'0',original:{lineId:'4', isActive:false, length: '3'}},
{id:'1',original:{lineId:'5', isActive:true, length: '4'}},
{id:'2',original:{lineId:'6', isActive:false, length: '7'}}
];
I tried to use .map() but I do something wrong. Can You please suggest where is the issue ?
const newTab = tab.map(x => x.original.isActive === false ? 'off' : x.original.isActive === true ? 'on' : x);
This is how newTab should look like after change:
const newTab =
[
{id:'0',original:{lineId:'4', isActive:'off', length: '3'}},
{id:'1',original:{lineId:'5', isActive:'on', length: '4'}},
{id:'2',original:{lineId:'6', isActive:'off', length: '7'}}
];
With current code it returns changed isActive property only:
const newTab = ["off", "on", "off"]
thanks !
I have const tab where I want to change property isActive from boolean to 'on' or 'off' depending on if it's true or false.
const tab = [
{id:'0',original:{lineId:'4', isActive:false, length: '3'}},
{id:'1',original:{lineId:'5', isActive:true, length: '4'}},
{id:'2',original:{lineId:'6', isActive:false, length: '7'}}
];
I tried to use .map() but I do something wrong. Can You please suggest where is the issue ?
const newTab = tab.map(x => x.original.isActive === false ? 'off' : x.original.isActive === true ? 'on' : x);
This is how newTab should look like after change:
const newTab =
[
{id:'0',original:{lineId:'4', isActive:'off', length: '3'}},
{id:'1',original:{lineId:'5', isActive:'on', length: '4'}},
{id:'2',original:{lineId:'6', isActive:'off', length: '7'}}
];
With current code it returns changed isActive property only:
const newTab = ["off", "on", "off"]
thanks !
Share Improve this question asked Mar 22, 2022 at 22:09 marcinb1986marcinb1986 9022 gold badges22 silver badges38 bronze badges3 Answers
Reset to default 2You can use spread syntax, and you can update variables in map method.
tab.map((e) => (
{
...e,
original: {
...e.original,
isActive: e.original.isActive ? 'on' : 'off'
}
}
))
You want to return the entire object, not just on
or off
inside your map. You can condense this using ...
(spread operator) and only modify isActive
while returning everything else as is:
const tab = [{
id: '0',
original: {
lineId: '4',
isActive: false,
length: '3'
}
},
{
id: '1',
original: {
lineId: '5',
isActive: true,
length: '4'
}
},
{
id: '2',
original: {
lineId: '6',
isActive: false,
length: '7'
}
}
];
const newTab = tab.map(item => ({
...item,
original: {
...item.original,
isActive: item.original.isActive ? 'on' : 'off',
}
}));
console.log(newTab)
you can use a mapping and return a JSON by copying the values.
const tab = [
{ id: '0', original: { lineId: '4', isActive: false, length: '3' } },
{ id: '1', original: { lineId: '5', isActive: true, length: '4' } },
{ id: '2', original: { lineId: '6', isActive: false, length: '7' } }
];
const tab2 = tab.map(e => {
return {
id: e.id,
original: {
lineId: e.original.lineId,
isActive: e.original.isActive ? 'on' : 'off',
length: e.original.length
}
}
});
Output : Image Output
I have const tab where I want to change property isActive from boolean to 'on' or 'off' depending on if it's true or false.
const tab = [
{id:'0',original:{lineId:'4', isActive:false, length: '3'}},
{id:'1',original:{lineId:'5', isActive:true, length: '4'}},
{id:'2',original:{lineId:'6', isActive:false, length: '7'}}
];
I tried to use .map() but I do something wrong. Can You please suggest where is the issue ?
const newTab = tab.map(x => x.original.isActive === false ? 'off' : x.original.isActive === true ? 'on' : x);
This is how newTab should look like after change:
const newTab =
[
{id:'0',original:{lineId:'4', isActive:'off', length: '3'}},
{id:'1',original:{lineId:'5', isActive:'on', length: '4'}},
{id:'2',original:{lineId:'6', isActive:'off', length: '7'}}
];
With current code it returns changed isActive property only:
const newTab = ["off", "on", "off"]
thanks !
I have const tab where I want to change property isActive from boolean to 'on' or 'off' depending on if it's true or false.
const tab = [
{id:'0',original:{lineId:'4', isActive:false, length: '3'}},
{id:'1',original:{lineId:'5', isActive:true, length: '4'}},
{id:'2',original:{lineId:'6', isActive:false, length: '7'}}
];
I tried to use .map() but I do something wrong. Can You please suggest where is the issue ?
const newTab = tab.map(x => x.original.isActive === false ? 'off' : x.original.isActive === true ? 'on' : x);
This is how newTab should look like after change:
const newTab =
[
{id:'0',original:{lineId:'4', isActive:'off', length: '3'}},
{id:'1',original:{lineId:'5', isActive:'on', length: '4'}},
{id:'2',original:{lineId:'6', isActive:'off', length: '7'}}
];
With current code it returns changed isActive property only:
const newTab = ["off", "on", "off"]
thanks !
Share Improve this question asked Mar 22, 2022 at 22:09 marcinb1986marcinb1986 9022 gold badges22 silver badges38 bronze badges3 Answers
Reset to default 2You can use spread syntax, and you can update variables in map method.
tab.map((e) => (
{
...e,
original: {
...e.original,
isActive: e.original.isActive ? 'on' : 'off'
}
}
))
You want to return the entire object, not just on
or off
inside your map. You can condense this using ...
(spread operator) and only modify isActive
while returning everything else as is:
const tab = [{
id: '0',
original: {
lineId: '4',
isActive: false,
length: '3'
}
},
{
id: '1',
original: {
lineId: '5',
isActive: true,
length: '4'
}
},
{
id: '2',
original: {
lineId: '6',
isActive: false,
length: '7'
}
}
];
const newTab = tab.map(item => ({
...item,
original: {
...item.original,
isActive: item.original.isActive ? 'on' : 'off',
}
}));
console.log(newTab)
you can use a mapping and return a JSON by copying the values.
const tab = [
{ id: '0', original: { lineId: '4', isActive: false, length: '3' } },
{ id: '1', original: { lineId: '5', isActive: true, length: '4' } },
{ id: '2', original: { lineId: '6', isActive: false, length: '7' } }
];
const tab2 = tab.map(e => {
return {
id: e.id,
original: {
lineId: e.original.lineId,
isActive: e.original.isActive ? 'on' : 'off',
length: e.original.length
}
}
});
Output : Image Output
本文标签: javascriptHow to change one property value in array with objectsStack Overflow
版权声明:本文标题:javascript - How to change one property value in array with objects - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745635463a2160421.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论