Related
I have an array like below: I want to remove origin: 0 property and add it value directly using Javascript es6 feature. How to remove same repeated property from a nested array.
const orginalData = {
name: {
origin: 0,
value: 'christi'
},
location: {
origin: 0,
value: 'Blr'
},
address: {
origin: 0,
value: [{
"streetAddress1": {
"origin": 0,
"value": '12th street'
},
"city1": {
"origin": 0,
"value": 'Maxwell'
}
},
{
"streetAddress2": {
"origin": 0,
"value": '10=]]]]]]]th street'
},
"city2": {
"origin": 0,
"value": 'Coxwell'
}
}
]
}
}
const finalData = {
name: 'christi',
location: 'Blr',
address: [{
streetAddress1: '10th street',
city1: 'Maxwell'
},
{
streetAddress2: '12th street',
city2: 'Coxwell'
}
]
}
You could create generic function like this. reduce the entries of an object to remove a level of nesting and update with nested value. If value as an array, recursively call the function on each object using map and get an array of restructured objects. This will work for any level of nesting
const orginalData={name:{origin:0,value:"christi"},location:{origin:0,value:"Blr"},address:{origin:0,value:[{streetAddress1:{origin:0,value:"12th street"},city1:{origin:0,value:"Maxwell"}},{streetAddress2:{origin:0,value:"10=]]]]]]]th street"},city2:{origin:0,value:"Coxwell"}}]}};
function restructure(obj) {
return Object.entries(obj).reduce((acc, [k, { value }]) => {
acc[k] = Array.isArray(value) ? value.map(restructure) : value;
return acc;
}, {})
}
const finalData = restructure(orginalData)
console.log(finalData)
If you're using NodeJs you could use omit-deep to remove any property you want, regardless of where it is within the object.
For example, this:
const omitDeep = require('omit-deep');
const data = {
name: { origin: 0, value: 'christi' },
location: { origin: 0, value: 'Blr' },
address: {
origin: 0,
value: [
{ streetAddress1: { origin: 0, value: '12th street' }, city1: { origin: 0, value: 'Maxwell' } },
{ streetAddress2: { origin: 0, value: '10=]]]]]]]th street' }, city2: { origin: 0, value: 'Coxwell' } }
]
}
};
const finalData = omitDeep(data, 'origin');
Produces this result:
{
name: { value: 'christi' },
location: { value: 'Blr' },
address: {
value: [
{ streetAddress1: { value: '12th street' }, city1: { value: 'Maxwell' } },
{ streetAddress2: { value: '10=]]]]]]]th street' }, city2: { value: 'Coxwell' } }
]
}
};
first if you want to edit your data, it can't be a const, so change const by let or var.
second
you can use for loop to do that, you can juste write a function or add a function to JSON object
// first logique as global function
function keepKey(data, keep) {
for(let key in data) data[key] = data[key][keep];
}
// second logique as global function
function removeKey(data, remove, assignRest) {
for(let key in data){
//get the item
let item = data[key];
if(typeof item === 'object'){
//if you put 'use strict' at the top you have to use a loop
let temp = {}, lastKey = '';
for(let itemKey in item){
if(itemKey !== remove){
if(assignRest === true) temp = item[itemKey];
else temp[itemKey] = item[itemKey];
}
}
data[key] = temp;
//else you can use directly delete
//delete item[remove];
}
}
}
// add the function to JSON object
JSON.keepKey = {...function...}
// or
JSON.removeKey = {...function...}
JSON.keepKey(orginalData, 'value');
// will give you
{name: 'christi',location: 'Blr',...}
JSON.removeKey(orginalData, 'value', true);
// will give you
{name: 'christi',location: 'Blr',...}
JSON.removeKey(orginalData, 'value', false);
// will give you
{name: {value : 'christi'},location: {value: 'Blr'},...}
const finalData = {
// ...originalData, uncommnent this if you have more originalData has props that you do not want to chnage.
name: originalData.name.value,
location: originalData.location.value,
address: originalData.address.value.map(item => {
const { origin, ...rest } = item;
return rest;
}),
};
This is just a copy of adiga's logic, made more explicit with extraneous identifiers and comments.
It's intended to help with understanding of the reduce method (and other JavaScript features) and of recursion.
const originalData = {
name: { origin: 0, value: 'christi' },
location: { origin: 0, value: 'Blr' },
address: {
origin: 0,
value: [
{
"streetAddress1": { "origin": 0, "value": '12th street' },
"city1": { "origin": 0, "value": 'Maxwell' }
},
{
"streetAddress2": { "origin": 0, "value": '10=]]]]]]]th street' },
"city2": { "origin": 0, "value": 'Coxwell' }
}
]
}
};
function restructure(obj) {
// Whether `restructure` is called directly or recursively, it builds and returns
// a new object.
return Object.entries(obj).reduce( (acc, curr, ind, arr ) => {
// Here, `entries` is a 2D array where each 'row' is a property and the two
// 'columns' are the property name and property value.
// We identify them explicitly below and assume that that the property value
// is an object with a subproperty called "value", which we also identify.
const propKey = curr[0], propVal = curr[1], subpropVal = propVal["value"];
// Logs the index (ie 'row' number) of the current property and its property name
//console.log(ind, propKey);
// Here, `acc` is the object we will return. We give `acc` a new property with
// the same name as the current property.
// If the "value" subproperty of the current property holds an array, the new
// property will hold an array of objects, each of which is a `restructure`d
// version of an object from the source array. (This can happen many times,
// restructuring nested objects from many nested arrays.)
// If not, the new property will have the same value as the "value" subproperty does
acc[propKey] = Array.isArray(subpropVal) ? subpropVal.map(restructure) : subpropVal;
// If this call to `restructure` was recursive, we will continue looping through
// the array we are currently processing.
// If this was the original call, we're done and log our `finalData` to the console.
return acc;
}, {})
}
const finalData = restructure(originalData);
console.log(finalData);
I have a list of properties like this
[{"ID":"0"},{"Day":""},{"Time":""},{"Type":"Both"},{"Status":"false"},
{"ID":"0"},{"Day":""},{"Time":""},{"Type":"Both"},{"Status":"false"}]
I would like to convert them to something like this
[{"ID":"0","Day":"","Time":"","Type":"Both","Status":"false"},
{"ID":"0","Day":"","Time":"","Type":"Both","Status":"false"}]
This is one problem that i face nearly everytime converting a form to json and submitting it to a controller with complex type.
You could check if an object contains the key ID and build a new object with all following objects.
var data = [{ ID: "0" }, { Day: "" }, { Time: "" }, { Type: "Both" }, { Status: "false" }, { ID: "0" }, { Day: "" }, { Time: "" }, { Type: "Both" }, { Status: "false" }],
result = data.reduce(function (r, o) {
if ('ID' in o) {
r.push(Object.assign({}, o));
} else {
Object.assign(r[r.length - 1], o);
}
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
That is one weird input,..
With the following I'm assuming there is always an ID, and it comes first. As that data structure has no way of splitting the properties.
var a = [{"ID":"0"},{"Day":""},{"Time":""},{"Type":"Both"},{"Status":"false"},
{"ID":"0"},{"Day":""},{"Time":""},{"Type":"Both"},{"Status":"false"}];
var
ret = [],
c = null;
a.forEach((r) => {
if (r.ID) { c = {}; ret.push(c); }
let k = Object.keys(r)[0];
c[k] = r[k];
});
console.log(ret);
Given the following array:
[
{
"real":104.1530776708426,
"workHour":8,
"value":null
},
{
"real":71.53948769310401,
"workHour":9
},
{
"real":97.84076993321577,
"workHour":10
},
{
"real":115.72564185649178,
"workHour":11
},
{
"real":79.95589800993977,
"workHour":12
},
{
"real":91.52846219558896,
"workHour":13
},
{
"real":57.86282092824589,
"workHour":14
},
{
"real":148.33923183423036,
"workHour":15
},
{
"real":125.19410346293202,
"workHour":16
},
{
"real":67.33128253468612,
"workHour":17
},
{
"real":55.75871834903695,
"workHour":18
},
{
"real":102.04897509163365,
"workHour":19
},
{
"real":132.55846249016332,
"workHour":20
},
{
"real":138.87077022779013,
"workHour":21
},
{
"real":60,
"workHour":8
},
{
"real":52,
"workHour":9
},
{
"real":114,
"workHour":10
},
{
"real":115,
"workHour":11
},
{
"real":92,
"workHour":12
},
{
"real":102,
"workHour":13
},
{
"real":54,
"workHour":14
},
{
"real":62,
"workHour":15
},
{
"real":133,
"workHour":16
},
{
"real":116,
"workHour":17
},
{
"real":106,
"workHour":18
},
{
"real":115,
"workHour":19
},
{
"real":115,
"workHour":20
},
{
"real":125,
"workHour":21
}
]
How can I find where the workHour match, and combine real there?
I did it with pure JS
const array = [{"real":104.1530776708426,"workHour":8,"value":null},{"real":71.53948769310401,"workHour":9},{"real":97.84076993321577,"workHour":10},{"real":115.72564185649178,"workHour":11},{"real":79.95589800993977,"workHour":12},{"real":91.52846219558896,"workHour":13},{"real":57.86282092824589,"workHour":14},{"real":148.33923183423036,"workHour":15},{"real":125.19410346293202,"workHour":16},{"real":67.33128253468612,"workHour":17},{"real":55.75871834903695,"workHour":18},{"real":102.04897509163365,"workHour":19},{"real":132.55846249016332,"workHour":20},{"real":138.87077022779013,"workHour":21},{"real":60,"workHour":8},{"real":52,"workHour":9},{"real":114,"workHour":10},{"real":115,"workHour":11},{"real":92,"workHour":12},{"real":102,"workHour":13},{"real":54,"workHour":14},{"real":62,"workHour":15},{"real":133,"workHour":16},{"real":116,"workHour":17},{"real":106,"workHour":18},{"real":115,"workHour":19},{"real":115,"workHour":20},{"real":125,"workHour":21}];
var map = {};
for (var i=0; i<array.length; i++) {
var obj = array[i],
id = obj.workHour;
if (id in map) { // we know this id already
// get the object and sum properties
map[id].real += obj.real;
} else // create a new one
map[id] = {
workHour: id,
real: obj.real,
};
}
console.log(map)
How can I do it with ES6? or Underscore?
Loop the array with Array#reduce to create a new object with combined values:
var data = [{"real":104.1530776708426,"workHour":8,"value":null},{"real":71.53948769310401,"workHour":9},{"real":97.84076993321577,"workHour":10},{"real":115.72564185649178,"workHour":11},{"real":79.95589800993977,"workHour":12},{"real":91.52846219558896,"workHour":13},{"real":57.86282092824589,"workHour":14},{"real":148.33923183423036,"workHour":15},{"real":125.19410346293202,"workHour":16},{"real":67.33128253468612,"workHour":17},{"real":55.75871834903695,"workHour":18},{"real":102.04897509163365,"workHour":19},{"real":132.55846249016332,"workHour":20},{"real":138.87077022779013,"workHour":21},{"real":60,"workHour":8},{"real":52,"workHour":9},{"real":114,"workHour":10},{"real":115,"workHour":11},{"real":92,"workHour":12},{"real":102,"workHour":13},{"real":54,"workHour":14},{"real":62,"workHour":15},{"real":133,"workHour":16},{"real":116,"workHour":17},{"real":106,"workHour":18},{"real":115,"workHour":19},{"real":115,"workHour":20},{"real":125,"workHour":21}];
var result = data.reduce(function(r, o) {
if (r[o.workHour]) {
r[o.workHour].real += o.real
} else {
r[o.workHour] = {
workHour: o.workHour,
real: o.real
}
}
return r;
}, {});
console.log(result);
Using lodash(/underscore), this uses _.reduce() with _.clone() to build an aggregate object. The values of this object are then output as an array of objects similar to your input array using _.values().
var data = [{"real":104.1530776708426,"workHour":8,"value":null},{"real":71.53948769310401,"workHour":9},{"real":97.84076993321577,"workHour":10},{"real":115.72564185649178,"workHour":11},{"real":79.95589800993977,"workHour":12},{"real":91.52846219558896,"workHour":13},{"real":57.86282092824589,"workHour":14},{"real":148.33923183423036,"workHour":15},{"real":125.19410346293202,"workHour":16},{"real":67.33128253468612,"workHour":17},{"real":55.75871834903695,"workHour":18},{"real":102.04897509163365,"workHour":19},{"real":132.55846249016332,"workHour":20},{"real":138.87077022779013,"workHour":21},{"real":60,"workHour":8},{"real":52,"workHour":9},{"real":114,"workHour":10},{"real":115,"workHour":11},{"real":92,"workHour":12},{"real":102,"workHour":13},{"real":54,"workHour":14},{"real":62,"workHour":15},{"real":133,"workHour":16},{"real":116,"workHour":17},{"real":106,"workHour":18},{"real":115,"workHour":19},{"real":115,"workHour":20},{"real":125,"workHour":21}];
var result = _.values(_.reduce(data, (sumObj, curr) => {
if (sumObj[curr.workHour])
sumObj[curr.workHour].real += curr.real;
else
sumObj[curr.workHour] = _.clone(curr);
return sumObj;
}, {}));
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
I'm having an array of object,in which I'm storing the billkey and billvalue as attributes. I want billkey to be the key and billvalue to be the value of that particular key.
var log=[
{
billkey:"Name",
billvalue:"ABC"
},
{
billkey:"Department",
billvalue:"Computer"
}
{
billkey:"Name",
billvalue:"XYZ"
},
{
billkey:"Department",
billvalue:"Electrical"
}];
And I want to convert it into this format:
var log=[
{
Name:"ABC",
Department:"Computer"
},
{
Name:"XYZ",
Department:"Electrical"
}];
How about this simple solution. Hope it helps!
var log=[
{
billkey:"Name",
billvalue:"ABC"
},
{
billkey:"Department",
billvalue:"Computer"
},
{
billkey:"Name",
billvalue:"XYZ"
},
{
billkey:"Department",
billvalue:"Electrical"
}];
var arr = [];
var finalObj = [];
for(var i in log){
var someObject = log[i];
for(var j in someObject){
arr.push(someObject[j]);
}
}
for(var k = 0; k < arr.length; k+=4){
finalObj.push({
Name: arr[k+1],
Department: arr[k+3]
});
}
console.log(finalObj);
create the result using forloop
// store the values
var logs=[];
var log=[
{
billkey:"Name",
billvalue:"ABC"
},
{
billkey:"Department",
billvalue:"Computer"
},
{
billkey:"Name",
billvalue:"XYZ"
},
{
billkey:"Department",
billvalue:"Electrical"
},
];
loop the first array
for (i = 0; i < log.length; i++) {
// create empty variable for storing the values
var index = new Array();
// insert the first index value to key
index[log[i].billkey] = log[i].billvalue
// insert the second index value to key
index[log[i+1].billkey] = log[i+1].billvalue
// insert the result in to new array
logs.push(index);
// increment the i with 1
i=i+1;
}
console.log(logs);
You could use Array#reduce and use the remainder operator as witch for using either the last object or create a new one.
var log = [{ billkey: "Name", billvalue: "ABC" }, { billkey: "Department", billvalue: "Computer" }, { billkey: "Name", billvalue: "XYZ" }, { billkey: "Department", billvalue: "Electrical" }],
result = log.reduce(function (r, a, i) {
var o = {};
if (i % 2) {
r[r.length - 1][a.billkey] = a.billvalue;
} else {
o[a.billkey] = a.billvalue;
r.push(o);
};
return r;
}, []);
console.log(result);
I need to search for an element in a json object like this:
var new_a = 'new';
var json = { cells:
[ { type: 'model',
id: '5aef826a1809',
attrs: {
text: 'first',
a: 'changethis'
}
},
{ type: 'model',
id: '2c11b8bd8112',
attrs: {
text: 'second'
}
}
]
}
Now I want to find the object with the id = 5aef826a1809, because I want to change the value of a to new_a or insert an a-element if it doesn't exist.
If id = 2c11b8bd8112 a new a-element with the content new_a should be added.
I tried to use
var res = _.find(json.cells, { id: '5aef826a1809' }); // doesn't work
res.attrs.a = new_a; // this would update or add new field, right?
But this doesn't work
Try like this
var new_a = 'new';
var json = {
cells: [{
type: 'model',
id: '5aef826a1809',
attrs: {
text: 'first',
a: 'changethis'
}
}, {
type: 'model',
id: '2c11b8bd8112',
attrs: {
text: 'second'
}
}]
};
var obj = json.cells.find(function (x) {
return x.id == '5aef826a1809'
});
if (obj) { // return value if found else return undefined if not found
obj.attrs.a = new_a;
console.log(json);
}
JSFIDDLE
Using this simple for loop should work, if that's what you're looking for:
for(var i = 0; i<json.cells.length; i++){
if(json.cells[i].id == "5aef826a1809" || json.cells[i].id == "2c11b8bd8112"){
json.cells[i].attrs.a = "new_a";
}
}
Hope it helped.
You can use Array.prototype.some for searching, changing and for making a short circuit to prevent more iteration than necessary.
var new_a = 'new',
json = {
cells: [{
type: 'model',
id: '5aef826a1809',
attrs: {
text: 'first',
a: 'changethis'
}
}, {
type: 'model',
id: '2c11b8bd8112',
attrs: {
text: 'second'
}
}]
};
function change(id) {
json.cells.some(function (a) {
var i = id.indexOf(a.id);
if (~i) {
a.attrs = a.attrs || {};
a.attrs.a = new_a;
id.splice(i, 1);
if (!id.length) {
return true;
}
}
});
}
change(['5aef826a1809', '2c11b8bd8112']);
document.write('<pre>' + JSON.stringify(json, 0, 4) + '</pre>');