I have and array added_collections and current state is:
added_collections
(2) [Array(2), Array(1)]
0: Array(2)
0: []
1: [{…}]
length: 2
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
I need to remove an product from collection so I write:
added_collections[0][1].splice(0, 1);
so the result I expect to get is :
added_collections
(2) [Array(2), Array(1)]
0: Array(2)
0: []
1: []
length: 2
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
[[Prototype]]: Array(0)
but instead that I get:
(2) [Array(2), Array(1)]
0: Array(0)
length: 0
[[Prototype]]: Array(0)
1: [Array(10)]
length: 2
Why its two empty arrays are merged?
When I follow steps in browser console everything is fine but when I run the same code to execute its not the same results and seems that 2 empty arrays missing
UPDATE:
this is the full code of added_collection[0]:
'[[[],[{"id":32620843270280,"product_id":4632299831432,"title":"3 / red","price":"249.00","sku":"DM-03-red-3","position":1,"inventory_policy":"deny","compare_at_price":"0.00","fulfillment_service":"manual"}]]]'
and I run js code:
let coll_no = 0;
let page_no = 1;
let product_no = 0;
added_collections[coll_no][page_no].splice(product_no, 1);
Works fine for me
const collection = [
[
[],
[{
"id": 32620843270280,
"product_id": 4632299831432,
"title": "3 / red",
"price": "249.00",
"sku": "DM-03-red-3",
"position": 1,
"inventory_policy": "deny",
"compare_at_price": "0.00",
"fulfillment_service": "manual"
}]
]
]
collection[0][1].splice(0, 1)
console.log(collection)
Related
This question already has answers here:
Getting key of each object inside array of objects into an array: Javascript
(4 answers)
Closed last year.
I'm trying to find all the keys that are in my array to figure out if some keys already exist in there. Tho when I try to put in the keys seperate like this:
console.log(this.UrlArray.find(({color_ids}) => color_ids));
It works, but if I try doing it with the variable with the actual keys in it:
console.log(this.UrlArray.find(({prefixEqual}) => prefixEqual));
It doesn't work. The values that belong to the keys aren't important, but I stil can't figure out how to do it.
Array example:
(3) [{…}, {…}, {…}]
0:
bd_shoe_size_ids: Array(2)
0: "6601"
1: "6598"
length: 2
[[Prototype]]: Array(0)
[[Prototype]]: Object
1:
color_ids: Array(2)
0: "6056"
1: "6044"
length: 2
[[Prototype]]: Array(0)
[[Prototype]]: Object
2:
manufacturer_ids: Array(2)
0: "5875"
1: "5866"
length: 2
[[Prototype]]: Array(0)
[[Prototype]]: Object
length: 3
[[Prototype]]: Array(0)
I asked something like this before, but I got linkes to answers that had nothing to do with what I'm trying to do and it closed.
As desired outputs I just wanna see bd_shoe_size_ids, color_ids and manufacturer_ids when I console log it.
const data = [
{ bd_shoe_size_ids: [1, 2] },
{ color_ids: [3, 4] },
{ manufacturer_ids: [5, 6] },
];
const output = data.map(Object.keys).flat();
console.log(output);
I have a problem. I have a list of data that I output grouped. Now I want to output the values e.g. erzeugenGes to a barchart. Chart modules itself works. Fail only at the grouped data for evaluation.
Enclosed my code and list
1,2,3,4,5,6,7,8,9,10,11,12 are the Month for the ChartLabels
1.
1: [{…}]
2: [{…}]
3: [{…}]
4: [{…}]
5: [{…}]
6: [{…}]
7: [{…}]
8: [{…}]
9: Array(1)
0: {jahr: 2020, monat: 9, erzeugungGes: 901.04, verbrauchGes: 952.22, eigenverbrauch: 515.24, …}
length: 1
__proto__: Array(0)
10: Array(2)
0:
bezogen: 252.18
eigenverbrauch: 201.62
eingespeist: 140.94
erzeugungGes: 342.53
jahr: 2019
monat: 10
verbrauchGes: 453.75
__proto__: Object
1:
bezogen: 232.07
eigenverbrauch: 174.13
eingespeist: 102.37
erzeugungGes: 276.48
jahr: 2020
monat: 10
verbrauchGes: 406.19
__proto__: Object
length: 2
__proto__: Array(0)
11: Array(1)
0: {jahr: 2019, monat: 11, erzeugungGes: 291.87, verbrauchGes: 761.16, eigenverbrauch: 238.38, …}
length: 1
__proto__: Array(0)
12: [{…}]
/// and Data
getData() {
this.ds.getAuswertungJahr().subscribe(res => {
this.daten = res;
this.data = this.daten.reduce((r, a) => { console.log("a", a); console.log('r', r); r[a.monat] = [...r[a.monat] || [], a]; return r; }, {}); console.log("group", this.data);
this.barChartData[0].data = [];
for (let i = 0; i < this.data.lenght; i++) {
this.barChartData[0].data.push(this.data[i]);
}
})
}
Thanks
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 4 years ago.
I have an array of objects like this. The result is shown by console.log()
(5) [{…}, {…}, {…}, {…}, {…}]
0:
name:"SOMEVALUE"
partyDetails"SOMEVALUE"
__proto__:Object
1:
name: "SOMEVALUE"
partyDetails: "SOMEVALUE"
__proto__:Object
2:
name:"SOMEVALUE"
partyDetails: "SOMEVALUE"
__proto__: Object
3:
name:"SOMEVALUE"
partyDetails:"SOMEVALUE"
__proto__: Object
4:
name:"SOMEVALUE"
partyDetails: "SOMEVALUE"
__proto__:Object
length:5
__proto__:Array(0)
After deleting from the object by this command,
delete $scope.excelInfo.columnDefs[3];
delete $scope.excelInfo.columnDefs[4];
I get this on console.log
(5) [{…}, {…}, {…}, empty × 2]
0:
name:"SOMEVALUE"
partyDetails:"SOMEVALUE"
__proto__:Object
1:
name:"SOMEVALUE"
partyDetails:"SOMEVALUE"
__proto__:Object
2:
name:"SOMEVALUE"
partyDetails:"SOMEVALUE"
__proto__:Object
length:5
__proto__:Array(0)
How you see there are empty × 2 in the last array of objects, after delete.
What should I do to remove this 2 empty rows?
This 2 empty row make my data wrong in the view.
Is there any good solution?
I do not want to check if data equal to null.
Thanks a lot
You can use splice() to remove last two object without setting undefined as it does when you use delete
var arr = [
{
name:"SOMEVALUE1"
},
{
name:"SOMEVALUE2"
},
{
name:"SOMEVALUE3"
},
{
name:"SOMEVALUE4"
},
{
name:"SOMEVALUE5"
}
];
arr.splice(3,2)
console.log(arr);
Or using delete you need to also filter the defined objects only,
var arr = [
{
name:"SOMEVALUE1"
},
{
name:"SOMEVALUE2"
},
{
name:"SOMEVALUE3"
},
{
name:"SOMEVALUE4"
},
{
name:"SOMEVALUE5"
}
];
delete arr[3];
delete arr[4];
var res = arr.filter(item => item);
console.log(res);
Use Array.splice
$scope.excelInfo.columnDefs.splice(3,2)
e.g.
var arr = [1,2,3,4,5];
arr.splice(3,2);
console.log(arr);
Array.prototype.splice() the array instead of using delete.
$scope.excelInfo.columnDefs.splice(3, 2);
// ^ ^
// | |
// | Number of elements to remove
// Index at which to start removing
splice() modifies the array in place, so there is not need to reassign it.
This is working for list:
> Array.prototype.slice.call([1,2,3])
> [1, 2, 3]
How to create list from this type of data {1: "1", 2: "2", 3: "3"} ? Are there other methods than iterating object with for ?
the Array.prototype.slice.call() is not working here and gives []
This came from data = Object {0: Object, 1: Object, 2: Object, total : '29'} and then i delete data['total'] and need to access to slice, pop and other methods of Array
You could use the keys and map the values.
var o = {1: "1", 2: "2", 3: "3"},
a = Object.keys(o).map(function (k) {
return o[k];
});
console.log(a);
console.log(Array.prototype.slice.call({1: "1", 2: "2", 3: "3", length:5}));
The question arised, why Array.prototype.slice.call does not work. Basically because a length property is missing.
Only properties, which can be used as indices are used.
var o1 = {1: "1", 2: "2", 3: "3"},
o2 = {1: "1", 2: "2", 3: "3", length: 5},
o3 = {1: "1", 2: "2", 3: "3", abc: 42, length: 5},
o4 = {0: "0", 1: "1", 2: "2", 3: "3"};
o4.length=Object.keys(o4).length;
console.log(Array.prototype.slice.call(o1));
console.log(Array.prototype.slice.call(o2));
console.log(Array.prototype.slice.call(o3));
console.log(Array.prototype.slice.call(o4));
Note: Dynamic length of properties-in-object could be assigned using Object.keys(OBJECT). The cause of getting undefined in console is missing property at index 0
var list = [];
for (item in obj) {
list.push(obj[item]);
}
//use list.push(parseInt(obj[item])) for number array
You can also do the following if you want it straight
Object.values(obj);
I have a multidimensional object in JavaScript with the following structure:
arg: Array[2]
2.0: "can_be_whatever"
3.1: Array[6]
1.0: Array[1]
1.0: Object
1. conc: false
2. followers: Array[1]
3. altVec: Array[0]
4. parents: Array[0]
5. regMsgs: Array[0]
6. shared: Array[0]
7. sharedWith: Array[0]
8. consistsof: Array[3]
1. 0: Array[1]
1. 0: "a"
2. length: 1
3. __proto__: Array[0]
2. 1: Array[1]
1. 0: "a"
2. length: 1
3. __proto__: Array[0]
3. 2: Array[1]
1. 0: "a"
2. length: 1
3. __proto__: Array[0]
4. length: 3
5. __proto__: Array[0]
9. unregMsgs: Array[0]
10. part: 0
11. __proto__: Object
I am quite new in JavaScript and i need to fetch the values "a" from the nested arrays. Can somebody propose something?
Frankly, this structure doesn't make any sense to me. I tried to "convert" it into a "real" JavaScript structure. I hope this gets close to what you meant:
var arg = [
"can_be_whatever",
[[{
conc: false,
followers: [null],
altVec: [],
parents: [],
regMsgs: [],
shared: [],
sharedWith: [],
consistsof: [["a"], ["a"], ["a"]],
unregMsgs: []
}], null, null, null, null, null]
];
You can then select the "a" values like so:
var consistsof = arg[1][0][0].consistsof;
for (var i = 0; i < consistsof.length; ++i) {
console.log(consistsof[i][0]);
}
In JavaScript you can select values from an array using [index] (just like in any other programming language) and properties from an object using .property (like in Java).