Sorting an array of objects by object key - javascript

How do I sort this array of objects by checking the objects' key?
const list = [
{ "firstValue": { "a": "1" } },
{ "secondValue": { "b": "1" } },
{ "thirdValue": { "c": "1" } },
{ "fourthValue": { "d": "1" } },
{ "fifthValue": { "e": "1" } }
]
So in this case I want it to be sorted like:
const list = [
{ "fifthValue": { "e": "1" } },
{ "firstValue": { "a": "1" } },
{ "fourthValue": { "d": "1" } },
{ "secondValue": { "b": "1" } },
{ "thirdValue": { "c": "1" } }
]
This is my current code:
sortArrOfObjectsByKey(list);
document.write(JSON.stringify(list))
function sortArrOfObjectsByKey(arrToSort) {
arrToSort.sort(function(a, b) {
return a < b ? -1 : 1
});
}
But it doesn't get sorted correctly.
Please note that there shouldn't be any new container for the new sorted array/object.
Help! Thanks

You need to fetch property name and then sort according to that
const list = [
{ "firstValue": { "a": "1" } },
{ "secondValue": { "b": "1" } },
{ "thirdValue": { "c": "1" } },
{ "fourthValue": { "d": "1" } },
{ "fifthValue": { "e": "1" } }
]
list.sort( function(a, b) {
const aKey = Object.keys(a)[0];
const bKey = Object.keys(b)[0];
return aKey.localeCompare(bKey)
} );
console.log(list)

Related

Change dynamic json structure to key,value and children structure

I have a dynamic JSON structure ex:
{
"video": {
"width": 1920,
"height": 1080,
"video_codec": "H264",
"CBR": "4337025",
"frame_rate": {
"numerator": 25,
"denominator": 1
},
"specified": {
"numerator": 1,
"denominator": 1
},
"gop": {
"length": 50,
"reference_frames": 3,
"sub_gop": "StaticType"
},
"codec_details": {
"profile": "Main",
"level": "Level4",
"entropy_encoding": "CABAC",
"video_output": "AVC1"
}
}
}
I want to transform it to tree node
export class TrackDetailsNode {
key: string;
value: string;
children?: TrackDetailsNode[];
}
Output Example:
{
"key": "video",
"children": [
{
"key": "width",
"value": "1920"
},
{
"key": "frameRate",
"children": [
{
"key": "numerator",
"value": "60"
},
{
"key": "denominator",
"value": "1"
}
]
}
]
}
I need it in a recursive way. I want to build a tree from the provided JSON.
Tried multiple solution but is taking a long time to parse.
var obj = {
"video": {
"width": 1920,
"height": 1080,
"video_codec": "H264",
"CBR": "4337025",
"frame_rate": {
"numerator": 25,
"denominator": 1
},
"specified": {
"numerator": 1,
"denominator": 1
},
"gop": {
"length": 50,
"reference_frames": 3,
"sub_gop": "StaticType"
},
"codec_details": {
"profile": "Main",
"level": "Level4",
"entropy_encoding": "CABAC",
"video_output": "AVC1"
}
}
};
function transform(data, output, children) {
Object.keys(data).forEach((key) => {
let value = data[key];
if (children) {
if (typeof value === 'object') {
const child = [];
children.push({
'key': key,
children: child
});
transform(value, undefined, child)
} else {
children.push({
'key': key,
value
})
}
} else {
if (typeof value === 'object') {
output.key = key;
output.children = [];
transform(value, undefined, output.children);
} else {
output.key = key;
output.value = value;
}
}
});
return output;
}
console.log(transform(obj, {}));
const INPUT = {
"video": {
"width": 1920,
"height": 1080,
"video_codec": "H264",
"CBR": "4337025",
"frame_rate": {
"numerator": 25,
"denominator": 1
},
"specified": {
"numerator": 1,
"denominator": 1
},
"gop": {
"length": 50,
"reference_frames": 3,
"sub_gop": "StaticType"
},
"codec_details": {
"profile": "Main",
"level": "Level4",
"entropy_encoding": "CABAC",
"video_output": "AVC1"
}
}
};
const isObject = (obj) => obj !== null && typeof obj === 'object';
const Transform = (input, isChildren) => {
const output = isChildren ? [] : {};
if (input && isObject(input)) {
Object.keys(input).forEach(key => {
const value = input[key];
const val = isObject(value) ? Transform(value, true) : value;
const valueKey = isObject(value) ? "children" : "value";
if (isChildren) {
output.push({
"key": key,
[valueKey]: val
});
} else {
output.key = key;
output[valueKey] = val;
}
})
}
return output;
}
const result = Transform(INPUT);
console.log(JSON.stringify(result, null, 2));

A function to obtain all leaf node properties of an object in an array of string in javascript

Request you to please help in building a function in javascript to obtain the mentioned output from the input given.
INPUT : An object (possibly a nested object)
example :
{
"message":"string" ,
"data1": {
"Output1": {
"leaf1": "abc",
"Leaf2": "123"
}
}
"data2": {
"Output2": {
"leaf3": "abc",
"leaf4": "123"
}
}
}
OUTPUT : An array of string
Example :
str= ["message", "data1.Output1.leaf1", "data1.Output1.leaf2" , "data2.Output2.leaf3","data2.Output2.leaf4"]
Something like this it will work
const getBranches = (data, prefix=[]) => {
if (typeof(data) !== 'object') {
return prefix.join('.')
}
return Object.entries(data).flatMap(([k, v]) => getBranches(v, [...prefix, k]))
}
const data = {
"message": "string",
"data1": {
"Output1": {
"leaf1": "abc",
"Leaf2": "123"
}
},
"data2": {
"Output2": {
"leaf3": "abc",
"leaf4": "123"
}
}
}
console.log(getBranches(data))
Second version
const data = {
"message": "string",
"data1": {
"Output1": {
"leaf1": [{
"b": {
"c": "12"
}
}]
}
},
"data2": {
"Output2": {
"leaf3": "abc",
"leaf4": "123"
}
}
}
const getBranches = (data, prefix = []) => {
if (typeof(data) !== 'object') {
return prefix.join('.')
}
return Object.entries(data).flatMap(([k, v]) => Array.isArray(data) ? getBranches(v, [...prefix]) : getBranches(v, [...prefix, k]))
}
console.log(getBranches(data))

How to sort an object of objects based on the keys in descending order?

I'm trying to order an array of object of objects based on the object's key.
How do I go about sorting a JSON Object that's already in ascending order? Please see below.
I've tried to convert the data value object into its own array, then sort it that way, but I'm getting syntax errors.
var object = [
A1: {
errors: {}
otherData: {}
data: {
"1": {
"name": "Ashley",
},
"2": {
"name": "Cardiff",
},
"3": {
"name": "Reading",
}
}},
A2: {
errors: {}
otherData: {}
data: {
"4": {
"name": "Susan",
},
"5": {
"name": "Bee",
},
"6": {
"name": "Bob",
}
}}];
I want it to be:
var object = [
A1: {
errors: {}
otherData: {}
data: {
"3": {
"name": "Reading",
},
"2": {
"name": "Cardiff",
},
"1": {
"name": "Ashley",
}
}},
A2: {
errors: {}
otherData: {}
data: {
"6": {
"name": "Bob",
},
"5": {
"name": "Bee",
},
"4": {
"name": "Susan",
}
}}];
If I understand correctly you want to sort the in alphabetical order by first letter. This is kind of out there but it should do what you're looking for
const arr1 = object.map(function(o) {
return Object.values(o[Object.keys(o)].data).sort((a, b) => a - b);
})
I'll try to explain what's happening here. map is iterating over each object in the array and return a brand new array. o[Object.keys(o)].data is selecting A1 and A2 data keys. Then we're saying we want the values of the objects in those data objects with the surrounds Object.keys() which is giving us the names. From there we're just calling sort and giving it the callback.
const sortByField = (field, isRevered = false, primerFn) => {
if (field) {
var key = primerFn ? (x) => primerFn(x[field]) : (x) => x[field];
isRevered = !isRevered ? 1 : -1;
return (a, b) => {
/*eslint-disable */
return a = key(a), b = key(b), isRevered * ((a > b) - (b > a));
/*eslint-enable */
}
}
else {
return (a, b) => {
return isRevered ? a < b : a > b;
}
}
}
var dataToSort = {
A1: {
errors: {},
otherData: {},
data: {
"1": { "name": "Ashley", },
"2": { "name": "Cardiff", },
"3": { "name": "Reading", }
}
},
A2: {
errors: {},
otherData: {},
data: {
"4": { "name": "Susan", },
"5": { "name": "Bee", },
"6": { "name": "Bob", }
}
}
};
const sortObjectByKeys = (obj) => {
let values = [];
let keys = [];
Object.keys(obj).forEach(key => {
keys.push(key);
values.push(obj[key])
})
values.sort(sortByField("name", true, (value) => value.toLowerCase()));
let sortedObject = {};
values.forEach((value, index) => {
sortedObject[keys[index]] = value;
})
return sortedObject;
}
let sortedData = {};
Object.keys(dataToSort).forEach(dataKey => {
sortedData[dataKey] = {
...dataToSort[dataKey],
data: sortObjectByKeys(dataToSort[dataKey].data)
}
})
console.log(JSON.stringify(sortedData));

setState on nested map break origin object structure in ReactJS

I have an array of objects, and I want to add/change a new property given it matches type and key.
[
{
"type": "a",
"units": [
{
"key": "keyofba"
},
{
"key": "mytargetkey"
}
]
},
{
"type": "b",
"units": [
{
"key": "keyofb"
}
]
},
{
"type": "ab",
"units": [
{
"key": "mytargetkey"
}
]
}
]
I tried this
this.setState({
schema: schema.map(s => {
if (s.type === 'a' || s.type === 'ab') { //hardcord for testing
return s.units.map(unit => {
if (unit.key === 'mytargetkey') {
return {
...unit,
newProp: 'newProp value'
}
} else {
return { ...unit }
}
})
} else {
return { ...s }
}
})
But somehow it doesn't work, I think I missed something, need spotter.
That's because you have to return the list modified inside the new object, and if not it the target, return the element as is:
schema.map(s => {
if (s.type === 'a' || s.type === 'ab') { //hardcord for testing
return {...s, units: s.units.map(unit => {
if (unit.key === 'mytargetkey') {
return {
...unit,
newProp: 'newProp value'
}
} else {
return unit
}
})}
} else {
return s
}
})

Parse the JSON data in javascript

I Have following JSON data, I need to iterate this data based on Keytype in JAVASCRIPT.
It should return record from only the passed key code.
Say i need record from 438 means it should give me only following data.
(ex:"K": "43800001", "D": "Data1")
{
"GroupCode": {
"PickType": [
{
"#KeyType": "438",
"R": [
{
"K": "43800001",
"D": "Data1"
}
]
},
{
"#KeyType": "439",
"R": [
{
"K": "43900001",
"D": "Data2"
}
]
},
{
"#KeyType": "440",
"R": [
{
"K": "44000001",
"D": "Data3"
}
]
},
{
"#KeyType": "441",
"R": [
{
"K": "44100001",
"D": "Data4"
}
]
}
]
}
}
as I'm not good in java script and new to it, i haven't tried coding for this. Please help me in getting the data.
Let us say the above is stored in a variable obj. You can get the result via following
var result = obj.GroupCode.PickType.filter(function(item){
return item["#KeyType"] === "438"
}).map(function(item){
return item.R[0];
});
Please note, result is an array. If you have unique object against the condition then for that you will have to check the length of array and then extract the object i.e. result[0] or result.shift()
Working Fiddle here
function getByKeyType(keytype) {
for(i in f.GroupCode.PickType) {
if(f.GroupCode.PickType[i].KeyType == keytype) {
return f.GroupCode.PickType[i].R[0];
}
}
return null;
}
alert(getByKeyType(438).K)
alert(getByKeyType(438).D)
Try this
function getByKeyType(keytype) {
for(i in f.GroupCode.PickType) {
if(f.GroupCode.PickType[i].KeyType == keytype) {
return f.GroupCode.PickType[i].R[0];
}
}
return null;
}
Try a simple jquery each loop and a if:
jquery:
$.each(obj.GroupCode.PickType,function(i,v){
if (v["#KeyType"] == 438) {
console.log(v.R)//v.R[0];
}
});
javascript:
for(v in obj.GroupCode.PickType) {
if (v["#KeyType"] == 438) {
console.log(v.R)//v.R[0];
}
}

Categories