I want to update array in the product "table" via PUT request. lets say users array (product has many users)
localhost:1337/api/products/2
i am sending data in body
{
"data": {
"users": [4,6,8] //i want to push here next user but in json it is impossible [...users, newUser]
}
}
the problem is that my previous values which was in the arrray are lost when i use put becouse this is how PUT works.
Why i need it? I use strapi and there relation is made via array. I just need to update array. But i dont know how to do this.
Maybe should i to use redux and push value to array in store and then update??
CodePudding user response:
You can update your values with the use of the spread operator.
a = {
"data": {
"users": [4,6,8]
}
}
UpdatedValues = {
...a, data: {
...a.data, users:
[...a.data.users, 'newValues']
}
}
console.log(UpdatedValues);
You can update your array prior and send that as a value to your API request.
oldArray = [1, 2, 3];
newArray = [...oldArray, "newValue"];
requestPayload = {
data: {
users: newArray,
},
};
console.log(requestPayload)