Let's start with a variable called data. It is an empty array of objects like this:
data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
]
This data structure cannot be changed and begins with no values.
Our task is to update this data array using a function called updateData. Here's the initial setup of the function:
function updateData(){
if(!data.length){
data.push(arguments)
}
else{
//this parts really confuse me
}
}
The goal of this function is to accept any number of arguments and apply certain rules for updating the data array:
- Update the object's value in the data array to match the argument's value if they share the same name.
- Add new arguments to the data array if none of the existing objects have the same name.
So, how should we implement this function?
updateData([
{name:'a',value:'aa'},
{name:'b',value:'bb'}
])
// expect data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
]
updateData([
{name:'a',value:'aa'},
{name:'b',value:'DD'},
{name:'c',value:'cc'}
] )
// expect data = [
{name:'a',value:'aa'},
{name:'b',value:'DD'},
{name:'c',value:'cc'}
]