Javascript: push() to inner Dictionary-like object? - javascript

Returning from a long break from JS, and cant figure out how to add to my data type.
Its like Like a Dictionary<string,Dictionary<string,string>>:
Data = [
{
id: "aa",
value: [
{ key: "this", value: "99" },
{ key: "that", value: "66" }
]
}
];
But How do I push() the inner and outer data types at the same time?

If you want to push to Data object, use Data.push(yourObject);
If you want to push to the inner object, use Data[0].value.push(yourObject).

Related

How can I change the key value in an object when creating an array value while using the map function in React?

I have the data of the array object in a variable called hi[0].child.
hi[0].child = [
{code: "food", name: "buger"},
{code: "cloth", name: "outher"},
{code: "fruit", name: "apple"},
]
What I want to do is create a variable called hello, change the key value of hi[0].child, and put it in the hello variable.
For example, the key value of code is changed to value and name is changed to label. The values ​​I want are as below.
expecter answer
hello = [
{label: "burger", value: "food"},
{label: "outher", value: "cloth"},
{label: "apple", value: "fruit"},
]
But when I use my code I get these errors.
Did not expect a type annotation here
so How can i fix my code?
this is my code
let hello = []
hi[0].child.map((v) => {
hello.push(label: v.name, value: v.code)
})
You've missed the curly brackets for the object, it should be hello.push({ label: v.name, value: v.code }).
Also, map is not the right method to be used here, use forEach instead or use the value returned from map (as shown below).
const data = [
{ code: "food", name: "buger" },
{ code: "cloth", name: "outher" },
{ code: "fruit", name: "apple" },
];
const updatedData = data.map((v) => ({ label: v.code, value: v.name }));
console.log(updatedData);
Change the code like this
let hello = []
hi[0].child.map((v) => {
hello.push({label: v.name, value: v.code})
})
You need to pass object

Check same occurrence of value in Array of Objects, then add another array in first found object

I have an array of objects, for example
arr = [
{
date: "2020-03-20T11:40:07.620Z",
name: "whatever",
id: "abc123"
},
{
date: "2020-03-21T11:21:07.620Z",
name: "whatever1",
id: "def455"
},
{
date: "2020-03-22T11:54:07.620Z",
name: "whatever2",
id: "abc123"
}
]
Actual data is more than this. I've simplified the array.
Here, id is the key which can be same in more than 1 array of objects, for example in 1st and 3rd id is same.
I want to check if more than 1 objects contain the same value (id). If yes, add another array (sameIdArray) in the first object where id is same (1st in this case) and this array will now contain all those objects where that same value (id) was found and remove them from the actual array. The final array structure will be something like this.
arr = [
{
date: "2020-03-20T11:40:07.620Z",
name: "whatever",
id: "abc123",
sameIdArray: [
{
date: "2020-03-22T11:54:07.620Z",
name: "whatever2",
id: "abc123"
}
]
},
{
date: "2020-03-21T11:21:07.620Z",
name: "whatever1",
id: "def455"
}
]
You can use the groupBy functionality. You can group your data by id and use it accordingly.
You can use libraries like underscore or lodash, if using JavaScript

Loop through an array of objects and get where object.field equals value

I'm currently working on a small application where I have to loop through an enormous array of objects. What would be the most efficient method to perform this?
var array = [
{
id: "1",
name: "Alpha"
},
{
id: "2",
name: "Beta"
},
...
];
I'd like to get each object where name equals "Alpha". I'm currently using a simple if statement to filter the objects with a different name value out, but I wonder if there's a more efficient way to do this, performance-wise.
It's worth to mention that I'll push the matching results into a new array.
No, there is no more efficient way.
The alternative is to build and maintain some kind of internal data structure which allows you to find the desired elements faster. As usual, the trade off is between the work involved in maintaining such a structure vs the time it saves you.
I don't have any way about which I would know it's more effective.
But if you had your objects ordered by name you could stop your search imideatly upon reaching an object whose name is not equal to "Alpha".
To find the first object you're looking for you can use binary search and from this Object you go up and down until at both ends you reach an object which isn't named "Alpha" or the end of array.
This is only a way of optimizing and will require time to sort the array and also will take more time when adding an element.
There's a JavaScript function exactly for this kind of task. Filter
From the Docs
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
Here is a small example by code for getting all element from array which has a certain 'name' field:
const arr = [
{name: 'Abc'},
{name: 'Xyz'},
{name: 'Lmn'},
{name: 'Xyz'},
{name: 'Xyz'}
];
let response = findByName('Xyz');
console.log(response);
function findByName (name) {
return arr.filter((element) => {
return element.name = name;
});
}
If you need more than one time a collection with a given name, you could use an object with the names as hashes and have instantly access to the items.
var array = [{ id: "1", name: "Alpha" }, { id: "2", name: "Beta" }, { id: "3", name: "Beta" }, { id: "4", name: "Gamma" }, { id: "5", name: "Beta" }, { id: "2", name: "Alpha" }],
hash = Object.create(null);
array.forEach(function (a) {
if (!hash[a.name]) {
hash[a.name] = [];
}
hash[a.name].push(a);
});
console.log(hash);
.as-console-wrapper { max-height: 100% !important; top: 0; }

find the duplicates in a list of objects and separate - in JavaScript

In a a dojo / javscript application
I am trying to figure the most efficient way to find the duplicates in this list of objects below:
{ item: "A", value: "one" },
{ item: "B", value: "two" },
{ item: "C", value: "three" },
{ item: "B", value: "two" },
{ item: "D", value: "five" },
{ item: "C", value: "three" }
so what I need as a finished product is:
not found list:
{ item: "A", value: "one" },
{ item: "D", value: "five" }
found list:
{ item: "B", value: "two" },
{ item: "C", value: "three" }
Also, I am using DOJO AMD so I know I have forEach, filter, some available to me
I know I can do a first pass to get the duplicate value:
var split = function( list )
{
var found = [];
var objectList = lang.clone( list );
var test = objectList.pop();
objectList.forEach( function( inner )
{
if( test.item == test.item )
{
found.push( test.item );
}
} );
return found;
};
If you use Java: Stop using a list. Use a Set. This solves all your problems. Sry, you might be disappointed, but the answer is that simple.
Your best approach would be storing the data correctly.
It's possible that you still need to store non-unique items, if that's so - continue using an ArrayList, but in addition, use the following:
Override the hashcode & equals function as shown in this link: What issues should be considered when overriding equals and hashCode in Java?
Then, use a Set (HashSet would probably be enough for you) to store all your objects. This data structure will disregard elements which are not unique to elements already inside the set.
Then, all you need to do is query the size of the set, and that gives you the amount of unique elements in the list.

Angular/JS: Json String to object

Have problem with converting string to object.
I'm getting from the server array of object and some of these objects contains other objects. Problem is that JS reads inner object like a string.
Data structure:
[
{
name: String("Name1"),
key: String("name1"),
value: String("some text")
},
{
name: String("Name2"),
key: String("name2"),
value: [
{
name: String("Object1"),
key: String("object1"),
value: [
{
name: String("Object1 Name3"),
key: String("object1.name3"),
value: Object()
}
]
},
{
name: String("Name4"),
key: String("name4"),
value: String()
},
]
},
{
name: String("Name5"),
key: String("name5"),
value: Number("44")
}
]
Object with Name2 contains other other object like value.
Problem that when I'm trying to display this object tree with Angular object with name ** Object1 Name3** is a string.
How I can convert all data that I'm getting from the server to OBJECT?
This object tree can have up to 5 nested objects and check value of each of them not a solution for me.

Categories