Accessing an object property from outside scope - javascript

So I have 4 jsons with that looks like this:
{
"group": "A",
"id": "50"
"person": [
{
"name": 'Joe',
"age": '29'
},
{
"name": 'Jessie',
"age": '27'
}
]
}
I used this function to create an array with all the people from 4 different json's files.
list.forEach(list => {
list.person.forEach(person => {
peopleArray.push(person);
});
})
The problem is, when I pick a position from that array, I want to be able to access the group and the ID as well for example:
console.log(peopleArray[1].group);
Is that possible? Or I would have to those values inside the person?

Just include those values in the person object
const data = {
group: "A",
id: "50",
person: [
{
name: 'Joe',
age: '29'
},
{
name: 'Jessie',
age: '27'
}
]
}
data.person.map(obj => ({...obj, group: data.group, groupId: data.id}))
The result is:
[
{
age: "29",
group: "A",
groupId: "50",
name: "Joe"
},
{
age: "27",
group: "A",
groupId: "50",
name: "Jessie"
}
]

Related

JS - manipulate variables on Object.assign

How can I manipulate variables inside - map.set(key, Object.assign({}, item));.
Array for example -
const arr = [
{
name: "daniel",
sum: "$100",
},
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "$100",
},
{
name: "daniel",
sum: "100",
},
];
desired output -
const arr = [
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "100",
},
];
How can I split for the example inside the assign?

How do i create a function that will change the property inside an object that is located inside of an array, in a JSON

so ive been currently stuck on a JavaScript assignment for hours now.
so ive been given a Json file with the following data :
{
"owner": "Jun31d",
"info": [{ "id": 1, "name": "bob", "flavour": "vanilla", "age": 43 },
{ "id": 2, "name": "Juneid", "flavour": "Chocolate", "age": 19 },
{ "id": 3, "name": "Patrick", "flavour": "Strawberry", "age": 25 },
{ "id": 4, "name": "Jean", "flavour": "Mint", "age": 31 },
{ "id": 5, "name": "Ahmad", "flavour": "vanilla", "age": 18 },
{ "id": 6, "name": "Ali", "flavour": "Bubblegum", "age": 19 },
{ "id": 7, "name": "Frank", "flavour": "Pistachio", "age": 23 }
]
}
The instruction for my assigment is the following :
Instruction
So from what ive understood i need to create a function that holds two string parameters, And ill need to change the property inside of my object that is located in an array to a new property.
And until now heres what i did :
'use strict'
const iceCream = require('./main.json')
let namespace = {
changeProp : function (newprop,oldprop) {
for (let oldprop in iceCream.info) {
}
}
}
I know it is not much at all, but i just want some help to know how can i move forward a little bit more on my assigment.
Any help is appreciated, thank you
You can easily replace the oldKey with newKey using map, Object.keys, and reduce
const data = {
owner: "Jun31d",
info: [
{ id: 1, name: "bob", flavour: "vanilla", age: 43 },
{ id: 2, name: "Juneid", flavour: "Chocolate", age: 19 },
{ id: 3, name: "Patrick", flavour: "Strawberry", age: 25 },
{ id: 4, name: "Jean", flavour: "Mint", age: 31 },
{ id: 5, name: "Ahmad", flavour: "vanilla", age: 18 },
{ id: 6, name: "Ali", flavour: "Bubblegum", age: 19 },
{ id: 7, name: "Frank", flavour: "Pistachio", age: 23 },
],
};
function changePropertyName(arr, oldName, newName) {
return arr.map((o) => {
return Object.keys(o).reduce((acc, curr) => {
curr === oldName ? (acc[newName] = o[oldName]) : (acc[curr] = o[curr]);
return acc;
}, {});
});
}
console.log(changePropertyName(data.info, "flavour", "bestFlavour"));
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
This is how you can do it. Easy, simple and nasty solution.
const data = {
owner: 'Jun31d',
info: [
{ id: 1, name: 'bob', flavour: 'vanilla', age: 43 },
{ id: 2, name: 'Juneid', flavour: 'Chocolate', age: 19 },
{ id: 3, name: 'Patrick', flavour: 'Strawberry', age: 25 },
{ id: 4, name: 'Jean', flavour: 'Mint', age: 31 },
{ id: 5, name: 'Ahmad', flavour: 'vanilla', age: 18 },
{ id: 6, name: 'Ali', flavour: 'Bubblegum', age: 19 },
{ id: 7, name: 'Frank', flavour: 'Pistachio', age: 23 }
]
};
const renameProperty = (a, e, n) =>
a.map((el) => {
el[n] = el[e];
delete el[e];
return el;
});
console.log(renameProperty(data.info, "id", "_id"));

Update value of object in array with values in other Array [duplicate]

This question already has answers here:
Update value of object with values in different array
(4 answers)
Closed 2 years ago.
I am trying to reproduce my original problem;
I have two arrays in react state Array 1 is an original array from Database and Array2 is an updated array is state.
Objective is to update only changed rates and not quantity (and other pararmters) back to the Database, hence i need to update the values of rates in object of Array1 with the values of the rates in object 2 for a the objects of Array1 matching with the objects in Array2.
Array1 = [{
id: 1,
name: IceCream,
details: [{ id: "12", name: "milk", quantity: "50", rate: "100" },
{ id: "13", name: "cream", quantity: "50", rate: "300" }]
},
{
id: 2,
name: Coffee,
details: [{ id: "14", name: "Coffee bean", quantity: "60", rate: "200" },
{ id: "15", name: "water", quantity: "60", rate: "300" }]
},
{
id: 3,
name: Tea,
details: [{ id: "16", name: "Tea leaf", quantity: "50", rate: "700" }]
}]
Array2 = [{
id: 1,
name: IceCream,
details: [{ id: "12", name: "milk", quantity: "50", rate: "500" },
{ id: "13", name: "cream", quantity: "50", rate: "700" }]
},
{
id: 2,
name: Coffee,
details: [{ id: "14", name: "Coffee bean", quantity: "60", rate: "800" },
{ id: "15", name: "water", quantity: "60", rate: "8000" }]
}]
You need to iterate over the objects in Array1 using .map, check if it exists in Array2 by id using .find. Then, iterate over the details and update the rate if it also exists in that of the second array:
let Array1 = [
{
id:1,
name: "IceCream",
details:[
{id:"12",name:"milk",quantity:"50",rate:"100"},
{id:"13",name:"cream",quantity:"50",rate:"300"}
]
},
{
id:2,
name:"Coffee",
details:[
{id:"14",name:"Coffee bean",quantity:"60",rate:"200"},
{id:"15",name:"water",quantity:"60",rate:"300"}
]
},
{
id:3,
name:"Tea",
details:[
{id:"16",name:"Tea leaf",quantity:"50",rate:"700"}
]
}
]
let Array2 = [
{
id:1,
name: "IceCream",
details:[
{id:"12",name:"milk",quantity:"50",rate:"500"},
{id:"13",name:"cream",quantity:"50",rate:"700"}
]
},
{
id:2,
name:"Coffee",
details:[
{id:"14",name:"Coffee bean",quantity:"60",rate:"800"},
{id:"15",name:"water",quantity:"60",rate:"8000"}
]
}
]
Array1 = Array1.map(item => {
let element = Array2.find(e => e.id == item.id);
if(element){
item.details = item.details.map(e => {
let detail = element.details.find(d => d.id==e.id);
if(detail)
e.rate = detail.rate;
return e;
});
}
return item;
});
console.log(Array1);

How to add a name for each element of a list in Jackson

Is it possible to set a name for each element of a list in JSON using JACKSON?
For example, I have the following JSON:
{"result": [
{
"name": "ABC",
"age": "20"
},{
"name": "DEF",
"age": "12"
}
]}
But I need this:
{"result": [
person: { // << this is the name
"name": "ABC",
"age": "20"
},
person: {
"name": "DEF",
"age": "12"
}
]}
Thanks to everybody!
UPDATE
Hi people!
I made a mistake! The correct form is the following:
{"result": [
{
person: { // << this is the name
"name": "ABC",
"age": "20"
}
},
{
person: {
"name": "DEF",
"age": "12"
}
}
]}
In plain Javascript, you could use Array#map and return a new object with person property.
var object = { result: [{ name: "ABC", age: "20" }, { name: "DEF", age: "12" }] };
object.result = object.result.map(function (a) {
return { person: a };
});
console.log(object);

linq.js Group By then Group By, How can it be achieved?

I want something that would result in this
[{
firstName: "Sam",
lastName: "Smith"
}, {
firstName: "Sam",
lastName: "Doe"
}, {
firstName: "Sam",
lastName: "Doe"
}, {
firstName: "Sam",
lastName: "Joe"
}]
Being something like this, with the exception of the number of columns to group with which is not fixed and the order of the columns as well
[{
"Sam": [{
"Smith": [{
firstName: "Sam",
lastName: "Smith"
}]
}, {
"Doe": [{
firstName: "Sam",
lastName: "Doe"
}, {
firstName: "Sam",
lastName: "Doe"
}]
}, {
"Joe": [{
firstName: "Sam",
lastName: "Joe"
}]
}]
}]
I want to be able to build something like the following photo demonstrates
I tried several times but i can't get a grasp on it, please help me :).
I propose a different output structure. Your structure seems convenient at first glance, but in further processing it will prove unnecessarily difficult to handle.
I would suggest this generic structure:
[
{
key: "group 1",
items: [{
key: "group 1.1",
items: [ {}, {}, {} ]
}, {
key: "group 1.2",
items: [ {}, {} ]
}]
}, {
key: "group 2",
items: [{
key: "group 2.1",
items: [ {}, {}, [}, {} ]
}, {
key: "group 2.2",
items: [ {} ]
}]
}
]
You can create a structure like this relatively easily:
var grouped = Enumerable.From(input).GroupBy("$.firstName", "", function(key, e) {
return {
key: key,
items: e.GroupBy("$.lastName", "", function (key, e) {
return {
key: key,
items: e.ToArray()
};
}).ToArray()
};
}).ToArray();
when applied to this input:
var input = [{
firstName: "Sam",
lastName: "Smith"
}, {
firstName: "Sam",
lastName: "Doe"
}, {
firstName: "Sam",
lastName: "Doe"
}, {
firstName: "Sam",
lastName: "Joe"
},{
firstName: "John",
lastName: "Joe"
}];
results in
[
{
"key": "Sam",
"items": [
{
"key": "Smith",
"items": [
{
"firstName": "Sam",
"lastName": "Smith"
}
]
},
{
"key": "Doe",
"items": [
{
"firstName": "Sam",
"lastName": "Doe"
},
{
"firstName": "Sam",
"lastName": "Doe"
}
]
},
{
"key": "Joe",
"items": [
{
"firstName": "Sam",
"lastName": "Joe"
}
]
}
]
},
{
"key": "John",
"items": [
{
"key": "Joe",
"items": [
{
"firstName": "John",
"lastName": "Joe"
}
]
}
]
}
]
Edit: To allow dynamically configurable grouping and ordering, a more advanced approach must be taken:
// Enumerable, definition => Enumerable
function applyOrder(items, definition) {
var i, prop, prefix, suffix, orderFunc;
if (!items) return;
if (!definition) return items;
for (i = 0; i < definition.length; i++) {
// definition[i] is either "propertyName" or "propertyName DESC"
prop = (definition[i] + " ").split(" ");
prefix = i === 0 ? "OrderBy" : "ThenBy";
suffix = prop[1].toUpperCase() === "DESC" ? "Descending" : "";
orderFunc = prefix + suffix;
items = items[orderFunc]("$." + prop[0]);
}
return items;
}
// Enumerable, definition => Enumerable
function applyGroup(items, definition) {
if (!items) return;
if (!definition) return items;
items = applyOrder(items, definition.order);
if (!definition.group) return items;
return items.GroupBy("$." + definition.group, "", function (key, e) {
return {
group: definition.group,
key: key,
items: applyGroup(e, definition.then).ToArray()
};
});
}
// Array, definition => Array
function applyStructure(items, definition) {
if (!items) return;
if (!definition) return items;
return applyGroup(Enumerable.From(items), definition).ToArray();
}
used like this:
var result = applyStructure(companies, {
group: "country",
order: ["country"],
then: {
group: "city",
order: ["city"],
then: {
order: ["companyName DESC"]
}
}
});
live at: http://jsfiddle.net/Tomalak/xth6ayuo/

Categories

Resources