Updating value of multidimensional array of object
You could iterate the indices and update text
property at the end.
function update(child, indices, value) { indices.reduce((o, i) => o.child[i], { child }).text = value;}
var array = [{ text: "something", child: [{ text: "something child", child: [] }] }];
update(array, [0, 0], 'foo');
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
How to update multidimensional array value in PHP
You could use indices :
$cars[0][1] = 20;
Will update the second value ([1]
) of the first array ([0]
).
Full code :
$cars = array
(
array("Volvo",10),
array("BMW",10),
array("Saab",10),
array("Land Rover",10)
);
$cars[0][1] = 20;
print_r($cars);
outputs :
Array
(
[0] => Array
(
[0] => Volvo
[1] => 20
)
[1] => Array
(
[0] => BMW
[1] => 10
)
[2] => Array
(
[0] => Saab
[1] => 10
)
[3] => Array
(
[0] => Land Rover
[1] => 10
)
)
Updating multi-dimensional array with new key,value object if exists or create new
Use a variable to detect if you found an old item:
var found = false;
for(var x=0; x < mycart.length; x++){
if(mycart[x].id == newid ){
var tmpq = mycart[x].quantity;
mycart[x].quantity = parseInt(tmpq) + parseInt(q);
found = true;
break;
}
}
if (!found) {
mycart.push ({ id: newid, quantity: newquantity });
}
It would be simpler if you made mycart
an object whose keys are the IDs, so you wouldn't have to do a linear search. It would be like this:
var mycart = {
"1": { quantity: 10 },
"6": { quantity: 20 },
...
};
Then your code would be:
if (mycart.hasOwnProperty(newid)) {
mycart[newid] += newquantity;
} else {
mycart[newid] = { quantity: newquantity };
}
Update Value on Multi-dimensional Array?
With arrayFilters
works fine, I tested with your object example.
db.getCollection('temp').update({}, {
$set: {
"destinos.$[d].origenes.$[o].base_price":450
},
}, {
arrayFilters: [
{
"d.ciudad_id": "1",
},
{
"o.ciudad_id": "122"
}
]
})
And check this: https://www.php.net/manual/es/mongodb-driver-bulkwrite.update.php because arrayFilter
is an element in the options.
[
"_id" => new \MongoDB\BSON\ObjectId($ruta_id)
],
[
'$set' => [
"destinos.$[d].origenes.$[o].base_price" => 450
]
],
[
"arrayFilters" =>
[
["d.ciudad_id" => "1"],["o.ciudad_id" => "122"]
]
]
Javascript multidimensional array updating specific element
PROBLEM:
I'm betting that you have a one-dimensional array with strings stored in each. So your array actually looks like:
array (
[0] => '...',
[1] => '.X.',
[2] => '...'
)
When this is what you want:
array (
[0] => array (
[0] => '.',
[1] => '.',
[2] => '.'
),
[1] => array (
[0] => '.',
[1] => 'X',
[2] => '.'
),
[2] => array (
[0] => '.',
[1] => '.',
[2] => '.'
)
)
SOLUTION:
When constructing your 2D array, make sure you explicitly declare each entry in board
as an array. So to construct it, your code might look something like this:
board = new Array();
rows = 3;
for (var i = 0; i < rows; i++)
board[i] = new Array('.', '.', '.');
Javascript changing value in multidimensional array
The problem reside here :
wocheArray[i] = {x, y, count, id};
If you check your code you would find that this part is overriding.
Consider your case: If a duplicate is found.
Then what your for loop will do:-
1.) Set var count = 0;
2.) [Skipping to If condition where is the procedure to check]
//IF match found (at j = 2 location, and i = 1)
//then what happen is :
if(2>1 && hoursYy == hoursY && secondsX == secondsXx){
wocheArray[1].count = wocheArray[1].count + 1;
// from here the count of wocheArray[1] would be increased by 1, Suppose 1.
}
3) In else block it will again set count = 0;
4) Again you are trying to set the values of wocheArray[1] = {x, y, count, id};
//now wocheArray[1] count is set to 0.
So from here, you can see you are updating the values or overriding them which creating the undesired result.
You should not call this outside wocheArray[i] = {x, y, count, id};
You must call it inside else block to make it work correctly.
else{
x = ....
....
....
count = 0;
wocheArray[i] = {x, y, count, id};
}
However, I am curious about the using of count = 0;
inside else block as it does nothing here and var count =0;
inside the for block which just re-initializes count variable which is wasting memory or not a good practice. According to shown code sample.
Change values of multidimensional array JavaScript
You could use recursion to get/change the values;
function inscribe(arr, path, value){
if(path.length == 1){
arr[path[0]] = value;
}else{
var next = path.shift();
inscribe(arr[next], path, value);
}
}
Updating an element in Javascript 2D array updates entire column
The docs of Array.fill
are your friend here:
Note all elements in the array will be this exact value.
This is telling you that each one of the sub-arrays are actually the SAME array. Thus, if you change one, you change them all.
Looking at the code below, you'll see that the first comparison is false
, whereas the second comparison is true
console.log([false,false,false,false] ===[false,false,false,false])
const arr = new Array(4).fill([false,false,false,false]);
console.log(arr[0] === arr[1]);
Related Topics
Read Qrcode from a Web Page With Camera.
How to Get Dynamic Id Attribute for a Button Which Is Inside a 'While' Loop
How to Import a Script into .Vue File
How to Validate a Credit Card Number
Slick Slider - How to Keep the Active Pagination (Dot) Always Centered
How to Use Lodash to Remove Keys from a Json Object
Typeerror: Cannot Create Property '_Id' on String
Why Am I Getting an Expected an Assignment or Function Call and Instead Saw an Expression
How to Remove Duplicates Elements Between 2 Arrays in Angular
How to Display Base64 Encoded Pdf
How to Sort Json Array Elements in Descending Order
How to Check If Prevprops and Nextprops Are the Same in React
How to Load Script in React Component
Set Focus to the Next Input Element
Validate Phone Number With JavaScript