say I have a variable
data=[]
//this variable is an array of object like
data = [
{name:'a',value:'aa'},
{name:'b',value:'bb'}
]
// the data structrue can not be changed and the initial value is empty
now I want to update the data
function updateData(){
if(!data.length){
data.push(arguments)
}
else{
//this parts really confuse me
}
}
this function must accept any number of arguments,and the order of the objects in data dose not matters update rule:
- update the objects value to the arguments value if they have the same name.
- add the arguments to the data if none of the object in data have the same name.
how to write 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'}
]
{name:'a',value:'aa'}and{name:'b',value:'bb'}at many places