get value of a field from json - javascript

I have a JSON that looks like this:
{
"name": "A1",
"aaaaa": [
{
"name": "B1",
"teststr": [
{
"name": "C1",
"state": false,
},
{
"name": "C2",
"state": false,
}
]
},
{
"name": "B2",
"teststr": [
{
"name": "C3",
"state": false,
}
]
}
]
}
I am using JavaScript.
Now, within A1, I have to find a specific "testStr" with name "C1" and change its "state" from false to true. Keys "aaaaa" and "teststr" are unique. And all "teststr"s have unique name.
Can someone please help me with this?

You could go this way:
var smallObject = object.aaaaa.filter(function (o) {
return o.name === "B1";
})[0];
var smallerObject = smallObject.teststr.filter(function (o) {
return o.name === "C1";
})[0];
smallerObject.state = true;
Where object is the literal object that you mentioned in the problem definition.

Related

How do I set value of nested object to key of current object?

This array has the key to substitute with nested key of 'name'
const arr = ['status', 'user', ...] <-- This array contains key to be replaced with name
This is what my current response object is
[
{
"id": 11,
"location": "Mumbai",
"status": {
"name": "NEW"
},
"user": {
"name": "Rakesh"
}
}
]
How do I modify the above array of objects to this below
[
{
"id": 11,
"location": "Mumbai",
"status": "NEW",
"user": "Rakesh"
}
]
can try below code
const keys = ['status', 'user']
let arr = [
{
"id": 11,
"location": "Mumbai",
"status": {
"name": "NEW"
},
"user": {
"name": "Rakesh"
}
}
]
arr.map(a => keys.forEach(k => {
if(a[k] && a[k].name) a[k] = a[k].name
}));
console.log(arr);
I'd try this one:
const results = [
{
"id": 11,
"location": "Mumbai",
"status": {
"name": "NEW"
},
"user": {
"name": "Rakesh"
}
}, {
"id": 12,
"location": "Helsinki",
"status": {
"name": "NEW"
},
"user": {
"name": "Samuli"
}
}
];
const flattenObject = ([key, value]) => ((typeof value === 'object') ? {[key] : value[Object.keys(value)[0]]} : {[key]: value});
const reduceToSingleObject = (acc, b) => ({...acc, ...b});
const actualResults = results.map((result) => Object.entries(result).map(flattenObject).reduce(reduceToSingleObject));
console.log(actualResults);
Explanation:
flattenObject is a function to flatten structure of object inside object. This only takes the first prop (without knowing the name of the key). If you, for some reason, would need to flatten several key-values, then it'd need whole different kind of helper function to sort that out.
reduceToSingleObject is a function to put all the key-value pairs into a single object. This could have been done already in flattenObject function, but for the clarity, I separated it to a normal map - reduce pattern.
actualResults is the outcome where we go through all the entries of your original results.

Form a nested tree from an array of objects in javascript

So there is array of objects of below format
let inputs = [
{
"id": "614344d9d9c21c0001e6af2e",
"groupName": "Unassigned",
"parentGroup": "null"
},
{
"id": "614447da152f69c3c1d52f2e",
"groupName": "P1",
"parentGroup": "null"
},
{
"id": "614447da152f69c3c1d52f38",
"groupName": "K1",
"parentGroup": "C1"
},
{
"id": "614447da152f69c3c1d52f3e",
"groupName": "A2",
"parentGroup": "C2"
},
{
"id": "614447da152f69c3c1d52f40",
"groupName": "G1",
"parentGroup": "P2"
},
{
"id": "614447da152f69c3c1d52f46",
"groupName": "F1",
"parentGroup": "null"
},
{
"id": "614447da152f69c3c1d52f30",
"groupName": "P2",
"parentGroup": "null"
},
{
"id": "614447da152f69c3c1d52f36",
"groupName": "C2",
"parentGroup": "P1"
},
{
"id": "614447da152f69c3c1d52f3c",
"groupName": "A1",
"parentGroup": "C2"
},
{
"id": "614447da152f69c3c1d52f34",
"groupName": "C1",
"parentGroup": "P1"
},
{
"id": "614447da152f69c3c1d52f32",
"groupName": "P3",
"parentGroup": "null"
},
{
"id": "614447da152f69c3c1d52f3a",
"groupName": "K2",
"parentGroup": "C1"
},
{
"id": "614447da152f69c3c1d52f42",
"groupName": "GG1",
"parentGroup": "G1"
},
{
"id": "614447da152f69c3c1d52f44",
"groupName": "GGG1",
"parentGroup": "GG1"
}
]
i am trying to create a tree structure of format
{name:'p1',children:[{name:'c1':children:[]}]}
so i sorted all the elements of given array considering element with parentGroup as "null" to be at the top of the array.
let finalArr = [];
inputs.sort((a,b)=> (a.parentGroup === "null") ? -1 : 1);
And for each element of the inputs array, i was iterating below logic
inputs.forEach(ele => {
if(ele.parentGroup === "null"){
let child= {name:ele.groupName,children:[]};
finalArr.push(child);
}else{
finalArr.forEach(item => {
this.findNode(item,ele);
})
}
});
If the 'parentGroup' of element is "null", then create a leaf kind of obj and push the element to 'finalArr' array
Else, then iterate across all the elements of 'finalArr' over a recursion function
public findNode(ele, obj){
if(ele.children.length === 0){
if(ele.name === obj.parentGroup){
let child = {name:obj.groupName, children:[]};
ele.children.push(child);
}
}else{
let j = ele.children.length-1;
this.findNode(ele.children[j--],obj);
}
}
This recursion function will check the element has children or not, if no children, then compare the parentGroup of given obj, with name of element from 'FinalArr'.
if so ,push the current obj to the children of the element of finalArr.
else, that is, when children has more elements, the same recursion will be triggered until depth of the element is reached.
With this i tought i would make a tree structure with given inputs array, but when a parent has more children, of same level, this logic fails,
so the inputs array has 'c1' which is a child of 'p1', but nly the child 'c2' resides, not sure the what is that i missed.
You could take a standard algorithm for getting a tree with given data
const
getTree = (data, id, parent, root, fn = o => o) => {
var t = {};
data.forEach(o => ((t[o[parent]] ??= {}).children ??= []).push(Object.assign(t[o[id]] = t[o[id]] || {}, fn(o))));
return t[root].children;
},
data = [{ id: "614344d9d9c21c0001e6af2e", groupName: "Unassigned", parentGroup: "null" }, { id: "614447da152f69c3c1d52f2e", groupName: "P1", parentGroup: "null" }, { id: "614447da152f69c3c1d52f38", groupName: "K1", parentGroup: "C1" }, { id: "614447da152f69c3c1d52f3e", groupName: "A2", parentGroup: "C2" }, { id: "614447da152f69c3c1d52f40", groupName: "G1", parentGroup: "P2" }, { id: "614447da152f69c3c1d52f46", groupName: "F1", parentGroup: "null" }, { id: "614447da152f69c3c1d52f30", groupName: "P2", parentGroup: "null" }, { id: "614447da152f69c3c1d52f36", groupName: "C2", parentGroup: "P1" }, { id: "614447da152f69c3c1d52f3c", groupName: "A1", parentGroup: "C2" }, { id: "614447da152f69c3c1d52f34", groupName: "C1", parentGroup: "P1" }, { id: "614447da152f69c3c1d52f32", groupName: "P3", parentGroup: "null" }, { id: "614447da152f69c3c1d52f3a", groupName: "K2", parentGroup: "C1" }, { id: "614447da152f69c3c1d52f42", groupName: "GG1", parentGroup: "G1" }, { id: "614447da152f69c3c1d52f44", groupName: "GGG1", parentGroup: "GG1" }],
tree = getTree(data, 'groupName', 'parentGroup', null, ({ groupName: name }) => ({ name }));
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I think the issue is how finalArr is used to generate the html elements.
When doing console.log(finalArr) it looks like the code block below. So it seems to me like the code you have to build the structure of finalArr is working fine.
// console.log(finalArr)
[
{ "name": "P3", "children": [] },
{
"name": "P2",
"children": [
{
"name": "G1",
"children": [
{ "name": "GG1", "children": [
{ "name": "GGG1", "children": [] }
]
}
]
}
]
},
{ "name": "F1", "children": [] },
{
"name": "P1",
"children": [
{ "name": "C2", "children": [
{ "name": "A1", "children": [] }
]
}
]
},
{ "name": "Unassigned", "children": [] }
]
EDIT
As OP mentioned in the comment C1 was missing. I've introduced a root element that will have the finalArr as its children and changed the findNode to use a for loop instead of forEach. In this way we can also break when we find the node and not continue recursing.
As part of the initial sorting we will sort the inputs by parentGroup so we ensure that a childs parent is added in the tree structure before we try to find it with findNode.
I believe this produces the correct result:
inputs.sort((a, b) => (a.parentGroup === "null" ? -1 : 1));
// Sort by parentGroup
let root = inputs.pop();
let inputsDescending = [root];
let max = inputs.length * inputs.length;
let c = 0;
while (inputs.length > 0 && max > c) {
const child = inputs.pop();
const hasParentGroup = inputsDescending.find(
(parent) => parent.groupName === child.parentGroup
);
if (hasParentGroup || child.parentGroup === "null") {
inputsDescending.push(child);
} else {
inputs.unshift(child);
}
}
let rootEle = { name: "root", children: [] };
inputsDescending.forEach((obj) => {
if (obj.parentGroup === "null") {
let child = { name: obj.groupName, children: [] };
rootEle.children.push(child);
} else {
findNode(rootEle, obj);
}
});
function findNode(ele, obj) {
if (ele.name === obj.parentGroup) {
let child = { name: obj.groupName, children: [] };
ele.children.push(child);
return true;
} else {
const c = ele.children.length;
if (c > 0) {
for (let i = 0; c > i; i++) {
const found = findNode(ele.children[i], obj);
if (found) break;
}
}
}
}
const finalArr = rootEle.children;
Now finalArr looks like this:
[
{ "name": "Unassigned", "children": [] },
{
"name": "P1",
"children": [
{
"name": "C1",
"children": [
{ "name": "K1", "children": [] },
{ "name": "K2", "children": [] }
]
},
{
"name": "C2",
"children": [
{ "name": "A2", "children": [] },
{ "name": "A1", "children": [] }
]
}
]
},
{ "name": "F1", "children": [] },
{
"name": "P2",
"children": [
{ "name": "G1", "children": [
{ "name": "GG1", "children": [] }
]
}
]
},
{ "name": "P3", "children": [] }
]

How to filter object with array inside?

I'm trying to filter everything inside 'items' with 'name': "", without losing structure and values of fieldLabel and placeholder. Below is my current object:
result: {
"fieldLabel": "foo",
"placeholder": "foo",
"items": [
{
"name": "foobar"
},
"name": ""
},
{
"name": ""
}
]
}
I want the object to look like this after filtering:
result: {
"fieldLabel": "foo",
"placeholder": "foo",
​"items": [
​{
​"name": "foobar"
​}, ​ ​ ​
​]
}
Have a reference to the original object first.
const results = {
"fieldLabel": "foo",
"placeholder": "foo",
"items": [
{
"name": "foobar"
},
{
"name": ""
},
{
"name": ""
}
]
}
Create a new object from the reference. Use spread syntax for copying object. Then filter method for filtering
const newObject = {
...results,
items: results.items.filter(item => item.name)
}
You can filter the nested items array that have truthy name properties.
const result= {
"fieldLabel": "foo",
"placeholder": "foo",
"items": [
{
"name": "foobar"
},
{
"name": ""
},
{
"name": ""
}
]
};
result.items = result.items.filter(({ name }) => name);
console.log(result);
If you need to do this using an immutable pattern then shallow copy the object and nested properties that you are updating.
const result= {
"fieldLabel": "foo",
"placeholder": "foo",
"items": [
{
"name": "foobar"
},
{
"name": ""
},
{
"name": ""
}
]
};
const newresult = {
...result,
items: result.items.filter(({ name }) => name)
};
console.log(newresult);
console.log(result === newresult); // false since new object
Something like this?
Filter YOUROBJ.items to keep all entries where property name has a value that is not null/undefined/false
res.items = res.items.filter(e => e.name);
const obj = {
"fieldLabel": "foo",
"placeholder": "foo",
"items": [
{
"name": "foobar"
},
{
"name": ""
},
{
"name": ""
}
]
}
const res = Object.assign({}, obj); // copy object to not mutate the original one
res.items = res.items.filter(e => e.name); // filter array "items" to just keep items with property name
console.log(res);

How can i change the organization of my json?

Hey i tried to transform a json file (as a js object but I can not do it). Here is an example of my problem:
Input object
{
"peoples": [
{
"name": "Alain",
"nationality": "Italian"
},
{
"name": "John",
"nationality": "French"
},
{
"name": "FOO",
"nationality": "French"
}
]
}
Output object
{
"nationality": {
"french": {
"peoples": [{ "name": "John" }, { "name": "FOO" }]
},
"italian": {
"peoples": [{ "name": "Alain" }]
}
}
}
How can i do this ? maybe Lodash but i have not find any way to do this. Can anyone help me ?
You can use a simple reduce:
const obj = {
"peoples": [{
"name": "Alain",
"nationality": "Italian"
},
{
"name": "John",
"nationality": "French"
},
{
"name": "FOO",
"nationality": "French"
}
]
}
const output = obj.peoples.reduce((a, {nationality: n, ...rest}) => {
const x = a.nationality[n]
if (x) x.push(rest)
else a.nationality[n] = [rest]
return a
}, { nationality: {} })
console.log(output)
Note, I've used a spread operator to get the rest of the properties, so if you were to add more properties to each person, then those would be included in the new object.

Value substitution while accessing nested json object

This is my json object
{
"a1": {
"b1": {
"name": "Tim",
"status": "Completed"
}
"c1" {
"field1": "name",
"field2": "status"
}
}
I need to access the value Tim by getting the field key within c1.
For example, I need to get the value of a1.c1.field1 which gives me the value name1 , then I need to access the value tim by a1.b1.(value of a1.c1.field1)
I do not know how to do this. Can someone give the possible ways to accomplish this?
var a1 =
{
"b1":
{
"name": "Tim",
"status": "Completed"
},
"c1":
{
"field1": "name",
"field2": "status"
}
};
console.log(a1.b1[a1.c1.field1]);
Do fix the error in your json too ;)
You can access using square brackets. With the data you provided,
var data = {
"a1": {
"b1": {
"name": "Tim",
"status": "Completed"
},
"c1":{
"field1": "name",
"field2": "status"
}
}
};
You can achieve your requirement by accessing.
data.a1.b1[data.a1.c1.field1]
Your JSON is a little off, so it's corrected below. This is an example of how to retrieve the value of field1 in the c1 object (in the a1 object)
$(document).ready(function () {
var json = {
"a1":
{
"b1":
{
"name": "Tim",
"status": "Completed"
},
"c1":
{
"field1": "name",
"field2": "status"
}
}
};
console.log(json.a1.b1.name); // returns Tim
// or
console.log(json["a1"]["b1"]["name"]); // returns Tim
});
Is this what you're looking for?
The obvious way is to use a1.c1.field1 as a property accessor using the bracket notation.
var obj = {
"a1": {
"b1": {
"name": "Tim",
"status": "Completed"
},
"c1": {
"field1": "name",
"field2": "status"
}
}
};
console.log(obj.a1.c1.field1); // 'name'
console.log(obj.a1.b1[obj.a1.c1.field1]); // 'Tim'
or, more legibly,
var key = obj.a1.c1.field1;
var value = obj.a1.b1[key];
console.log(value); // 'Tim'

Categories