기본 데이터
const items = [
{id: 0, name: 'name0'},
{id: 1, name: 'name1'},
{id: 2, name: 'name2'}
];
const tempItem = { id: 1, name: 'name1' };
findIndex
let newItems = [...items];
const findIndex = newItems.findIndex((x) => x.id === tempItem.id);
newItems[findIndex].name = '테스트';
Object.values & filter
const result = Object.values(items).filter((item) => {
if (item.id === tempItem.id) {
return true;
}
});
Object.entries
export const TEST_TYPE = {
'01': 'test1',
'02': 'test2',
'03': 'test3',
};
{Object.entries(TEST_TYPE).map(([key, value], idx) => (
<Radio key={idx} value={key}>
{value}
</Radio>
))}
Object.keys
export const TEST_TYPE = {
'01': { value: '01', text: 'test1' },
'02': { value: '02', text: 'test2' },
'03': { value: '03', text: 'test3' },
};
{Object.keys(TEST_TYPE).map((key) => (
<Radio key={TEST_TYPE[key].value} value={TEST_TYPE[key].value}>
{TEST_TYPE[key].text}
</Radio>
))}
map
const newUnits = items.map((obj) => {
if (obj.id === tempItem.id) {
return { ...tempItem, MODE: 'E' };
}
return obj;
});
// TIP. react
this.setState({ units: newUnits }, this.handleSave);