angular multidimensional array assignment [duplicate] - javascript

This question already has answers here:
Add element to multidimensional array
(4 answers)
Closed 3 years ago.
Working with Angular 5.
What I am trying to do is to assign in array a multidimensional array.
What is working with PHP?
$arM = array();
$arM["20/02/2020"][1] = ["name"=> 'my name', "id"=> 1];
the output of this is:
Array
(
[20/02/2020] => Array
(
[1] => Array
(
[name] => my name
[id] => 1
)
)
)
In angular when I am doing this is not working. Example:
let data:any = [];
data["20/02/2020"][1] = myArray;
How can I achieve the same result of PHP's?

Like this maybe?
let data = {}; // Object (with keys), not array
data["20/02/2020"] = { name : "my name", id : 1 };
console.log(data);

let data:Object = {
"20/02/2020":[
{"name":"my name", "id": 1},
{"name":"my name2", "id": 2},
{"name":"my name3", "id": 3}
]
};

Related

findIndex of item in array of object where object property match [duplicate]

This question already has answers here:
findIndex() javascript array object
(2 answers)
How do I get the index of object in array using angular?
(5 answers)
Get objects position in array
(3 answers)
Closed 1 year ago.
I have an array of object as follow :
[ {label: "<p>Opacity | %<br/></p>", customRow: false, id: 0, items: Array(13)},
{label: "Brand_hs_id", customRow: false, id: 0, items: Array(13)},
{label: "<p>PPI |</p>", customRow: false, id: 0, items: Array(13)},
{label: "Brightness | %", customRow: false, id: 0, items: Array(13)
]
I want to findIndex of object where label value matches. for example if I pass the "Brightness | %" it should return 3.
I checked
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
but not sure how to do it in array of objects .
Try something like as follows:
let val = [{id:"1002", address:"usa"},
{id:"1004", address:"canada"},
{id:"1006", address:"sweden"}];
let index = val.findIndex(x => x.address === "canada");
console.log(index); //Here index will be returned
do it like this
const array1 = [{age:5}, {age:12},{age:8} , {age:130}, {age:44}];
const isLargeNumber = (element) => element.age > 13;
console.log(array1.findIndex(isLargeNumber));

Convert array values to objects [duplicate]

This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 1 year ago.
I have this code
const jsonArray = [1,2,3,4,5];
I want to convert it so I can get
{
"id" : 1
},
{
"id" : 2
},
{
"id" : 3
},
{
"id" : 4
},
{
"id" : 5
}
So I need to add "id" key for all the objects outputs
Try the map function
jsonObjectArray = jsonArray.map((ele) => {return {"id": ele}})
You can use map.
jsonArray.map(id=>({id}))
You could use Array.prototype.map() for that:
const jsonArray = [1,2,3,4,5];
const arrayofObjects = jsonArray.map(e => ({id: e}))
console.log(arrayofObjects);

Loop through json object to get values of particular key [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
0: {id: "7B5B201E-35AA-48A1-B919-002445319F8B", name: "Naman Sabarwal"}
1: {id: "EA6672BA-4F7A-4214-A37F-00716CE698C9", name: "me name"}
2: {id: "01F29920-9206-42DF-8151-00A6A080C501", name: "Nitesh Negi"}
I want to get a list such that the list contains only the name key values.
listOfNames = ['sonu singh','me name','harman jain']
How to get all the values of the key 'name'?
listOfNames = jsonValues.map(x=>x.name)
You can try as follow.
let data = [
{id: "7B5B201E-35AA-48A1-B919-002445319F8B", name: "Naman Sabarwal"},
{id: "EA6672BA-4F7A-4214-A37F-00716CE698C9", name: "me name"},
{id: "01F29920-9206-42DF-8151-00A6A080C501", name: "Nitesh Negi"}
]; // assume the data is in array
let result = data.map( d => d.name );
console.log(result);
if it is json object like
details = [{id:"",name:""},{id:"",name:""},{id:"",name:""}]
you can use map function like
function getnames(){
let names = []
details.map((detail)=>{
names.push({name:detail.name})
return names
}
to shorten this
let names = values.map(value=>return value.name)

Javascript nested array to single array without using loop [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 5 years ago.
I have JSON array like this:
var items = [
[{id:1,name:'test'},{id:2,name:'test'},...] ,
[{id:1,name:'test'},{id:2,name:'test'},...] ,
]
From that I need an array like this:
var newArray = [
{ id: 1, name: 'test' }, { id: 2, name: 'test'}, { id: 3, name: 'test'}, ...
]
Now I am using a for loop on items array and storing into newArray but is there any other method without using loops?
I love functional and new prototpes of array so here is my demo
var items = [
[{id:1,name:'test'},{id:2,name:'test'}] ,
[{id:1,name:'test'},{id:2,name:'test'}]
]
var ll = items.reduce(function(prev, current){return prev.concat(current)})
console.log(ll)
Demo 1: concat() and .apply()
Demo 2: reduce() and concat()
Demo 3: ... Spread Operator and concat()
Demo 1
var items = [
[{id:1,name:'test'},{id:2,name:'test'}] ,
[{id:1,name:'test'},{id:2,name:'test'}] ,
]
var flattened = [].concat.apply([], items);
console.log(flattened);
Demo 2
var items = [
[{id:1,name:'test'},{id:2,name:'test'}],
[{id:1,name:'test'},{id:2,name:'test'}]
];
var flattened = items.reduce(function(prev, curr) {
return prev.concat(curr);
});
console.log(flattened);
Demo 3
var items = [
[{id:1,name:'test'},{id:2,name:'test'}],
[{id:1,name:'test'},{id:2,name:'test'}]
];
var flattened = [].concat(...items);
console.log(flattened);

Sorting an array object by nested values within objects - Javascript [duplicate]

This question already has answers here:
Sorted a javascript array of objects by an object property
(6 answers)
Closed 5 years ago.
I currently have an array object (not sure if this is the accurate name) that is comprised of nested key value pairs. I want to be able to sort this by the values within the nested objects.
For example:
var ObjArray = [
{ id = 1,
info = {
number = 4,
name = "foo"
}
},
{ id = 4,
info = {
number = 12,
name = "bar"
}
},
{ id = 9,
info = {
number = 2,
name = "fizz"
}
}
];
So ideally I could sort this object based on the 'number' property, and the resulting array object would have sub objects ordered by the number value within the info.
I've found a similar question (Sorting an object of nested objects in javascript (maybe using lodash?)) but doesn't account for another level of nested objects.
The sorting function needed is
ObjArray.sort((a,b) => a.info.number - b.info.number);
This will sort them ascending
For descending :
ObjArray.sort((a,b) => b.info.number - a.info.number);
var ObjArray = [{
id: 1,
info: {
number: 4,
name: "foo"
}
},
{
id: 4,
info: {
number: 12,
name: "bar"
}
},
{
id: 9,
info: {
number: 2,
name: "fizz"
}
}
];
ObjArray.sort((a,b) => a.info.number - b.info.number);
console.log(ObjArray);

Categories