Angular/JS: Json String to object - javascript

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.

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

How to push string array to json object?

I am new to typescript. please help to push the data so here it goes
Here the story goes I have string array and i need to push it to the json object
interface LocalIds {
value: string;
label: string;
}
const localIds = [
{ value: 'test', label: 'test' },
{ value: 'test2', label: 'test2' },
];
////////////// HERE in string array that data is coming ///////////
const localIdentifiers: string[] = result.data.map((item: string) => item);
///////////// I want to push the string array data to json object with label & value//////
// I experimented alot but giving some type error and all I am not getting
localIds.push({ label: 'abc', value: 'abc' });
localIdentifiers.map(i => localIds.push(...localIds:{value:i,label:i}[])); //ERROR
Half of your code does nothing useful
result.data.map((item: string) => item) will do nothing
using map when not returning anything inside it is pointless. At very least use forEach instead. or even better....
You should use map with concat:
interface LocalIds {
value: string;
label: string;
}
const localIds = [
{ value: 'test', label: 'test' },
{ value: 'test2', label: 'test2' },
];
localIds.push({ label: 'abc', value: 'abc' });
const finalLocalIds = localIds.concat( result.data.map((i: string) => ({value:i,label:i})) );
Live example
Try fixing last line as following
replace ; with , and remove [] at the end
localIdentifiers.map(i => localIds.push(...localIds, {value:i,label:i}));
also, you dont need ...localIds, since it will duplicate current array every time element is pushed to array

Merge javascript objects in an array based on an array inside it

What is the best way to reorganize array into output? I need to merge all value keys (whether array or not) into objects sharing the same name key. There is something similar here, but that doesn't answer my question because I have arrays as well.
var array = [{
VULN: [{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"}]
}, {
VULN: [{random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}]
}, {
VULN: [{random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
}];
to
VULN=[{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"},{name:"asd3",value:"asd3"}, {random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}, {random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
Here is what you want:
var array = [{
VULN: [{ name: "asd1", value: "asd1" }, { name: "asd2", value: "asd2" }]
}, {
VULN: [{ name: "asd3", value: "asd3" }, { name: "asd4", value: "asd4" }]
}, {
VULN: [{ name: "asd5", value: "asd5" }, { name: "asd6", value: "asd6" }]
}];
console.log(array.map(x => x.VULN).flat())
Use flatMap:
array.flatMap(x => x.VULN);
Use lodash
lodash.flatten(array.map(x => x.VULN))

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

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

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

Categories