I want to modify one object that has a part of a value that I want in this case the letter after the dash, and I want to assign only the latter back to the target object, any ideal how to do this I want to be able to push the returned array back in to itemsObject or in to a new object ? so in final the items object should have a : 'A' , b : 'B'.
useEffect(() => {
// context.items = {a: '1-A', b :'2-B'}
let modify = Object.values(context.items).map((value) => {
return value.split('-');
});
// itemsObject = {a : '', b : ''}
SetLink(Object.assign(itemsObject, context.items /*or new obj that will have the new values*/));
}, []);
// End Result
// itemsObject = {a : 'A', b : 'B'}
You could take the keys from your original itemsObject and map each key to an array of the form [key, value], where the value is the value after the dash - from your context.items object at the particular key you're mapping. You can then wrap the mapped array of keys in a call to Object.fromEntries() which will build your new object for you:
const items = {a: '1-A', b :'2-B'}; // context.items
const itemsObject = {a: '', b: ''};
const res = Object.fromEntries(Object.keys(itemsObject).map(key => [
key,
items[key].split("-").pop()
]));
console.log(res); // setLink(res);