Javascript how do i dynamically find keys in my array? [duplicate] - javascript

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);

Related

Remove array in array using splice issues

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)

Filter out array as per matching properties using Javascript

I have a array with properties -
(5) [VectorNode, VectorNode, VectorNode, VectorNode, VectorNode]
0: VectorNode
id: "5:4"
name: "group_00"
x: (...)
y: (...)
__proto__: Object
1: VectorNode {id: "5:5"}
2: VectorNode {id: "5:6"}
3: VectorNode {id: "5:7"}
4: VectorNode {id: "5:8"}
length: 5
__proto__: Array(0)
Not all VectorNodes will have the property name as group_01. I want to filter out only those VectorNodes whose name matches this regex - "/_\d+/" which is basically any VectorNode with name - dsadsa_01, dgdfg_0, oopoo_0999
How should I do it?
You can use match() function which takes regex as argument
here is the link for more details:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
So you can do something like this:
arrayOfVectorNodes.filter(_ => !!_.name.toLocaleLowerCase().match(yourRegex));
you can filter your array by using array.filter() method
let x = [{name:"group_00"},{name:"group_01"},{name:"group_abc"}];
const regex = /_\d+/;
let y = x.filter(function({name})=>{
return regex.test(name)
})
console.log(y)

Difference between 2 expression, one with spread and another without spread? [duplicate]

This question already has answers here:
Why do I need to copy an array to use a method on it?
(4 answers)
What is the difference between "Array(n)" and "[...Array(n)]"?
(1 answer)
Closed 3 years ago.
Using this object for an example
ingredients = {
salad: 3,
bacon: 1,
cheese: 1,
meat: 0
}
And I have 2 expressions which have different result one without spread operator
Object.keys(ingredients).map(igKey=>{
return [Array(3)]
})
and another with spread operator
Object.keys(ingredients).map(igKey=>{
return [...Array(3)]
})
Result without spread operator
(4) [Array(1), Array(1), Array(1), Array(1)]
0: [Array(3)]
1: [Array(3)]
2: [Array(3)]
3: [Array(3)]
Result with spread operator
(4) [Array(3), Array(3), Array(3), Array(3)]
0: (3) [undefined, undefined, undefined]
1: (3) [undefined, undefined, undefined]
2: (3) [undefined, undefined, undefined]
3: (3) [undefined, undefined, undefined]
I read spread operator is used to combine multiple arrays, what operation does spread operator does in this example?

Delete from array of object [duplicate]

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.

multidimensional arrays javascript

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).

Categories

Resources