Hi actually i have two arrays ( services and offer) and i'm trying connect offer array with service array based on service id using reduce method and i tried different method but nothing working i'm not able to solve this check my js fiddle link (not able to connect with offer array). please help me to resolve this. thanks in advance
const services = [
{
cid:1,
catname:'Facial',
services:[
{
sid:30,
sname:'Fruit facial'
},
{
sid:33,
sname:'Herbal facial'
}
]
},
{
cid:2,
catname:'Massage',
services:[
{
sid:40,
sname:'Head Massage'
},
{
sid:45,
sname:'Back Massage'
},
{
sid:46,
sname:'Face Massage'
}
]
}
]
Offer Array - here based on service id (sid) i want to connect with services array and create new array
const offer = [
{
offid:1,
sid:'33,40'
offvalue : 10%
},
{
offid:2,
sid:'45,46',
offvalue : 100
}
]
Expecting Result:
const Result = [
{
cid:1,
catname:'Facial',
services:[
{
sid:30,
sname:'Fruit facial'
},
{
sid:33,
sname:'Herbal facial',
offid:1,
offvalue : 10%
}
]
},
{
cid:2,
catname:'Massage',
services:[
{
sid:40,
sname:'Head Massage',
offid:1,
offvalue : 10%
},
{
sid:45,
sname:'Back Massage',
offid:2,
offvalue : 100
},
{
sid:46,
sname:'Face Massage',
offid:2,
offvalue : 100
}
]
}
]
If you can use ES6 features, then this should work:
const services = [{
cid:1,
catname:'Facial',
services: [{
sid:30,
sname:'Fruit facial'
}, {
sid:33,
sname:'Herbal facial'
}],
}, {
cid:2,
catname:'Massage',
services: [{
sid:40,
sname:'Head Massage'
}, {
sid:45,
sname:'Back Massage'
}, {
sid:46,
sname:'Face Massage'
}],
}];
const offer = [{
offid:1,
sid:'33,40',
offvalue : "10%"
}, {
offid:2,
sid:'45,46',
offvalue : "100"
}];
const result = services.map(val => {
return {
...val,
services: val.services.map(serv => {
// Here is where we find any matching offers, then add their data to the service.
const off = offer.find(x => x.sid.split(",").includes(String(serv.sid)));
if (off) {
serv.offid = off.offid;
serv.offvalue = off.offvalue;
}
return serv;
}),
}
});
console.log(result);
Related
I have below object structure in array
[
{
"modules":set of modules here
"details": [
{
"name":"abc"
"version":"1.2.3"
},
{
"name":"def"
"version":"2.3.4"
},
{
"name":"ghi"
"version":"4.5.6"
}
]
},
{
"modules":set of modules here
"details": [
{
"name":"jkl"
"version":"7.8.9"
},
{
"name":"mno"
"version":"10.11.12"
},
{
"name":"pqr"
"version":"13.14.15"
}
]
}
]
What I want to do is :
get details array transformed into below format for each root object in master array as
"details": [
{
module:"jkl:7.8.9"
},
{
module:"mno:10.11.12"
},
{
module:"pqr:13.14.15"
}
]
So final array would be :
[
{
"modules":set of modules here
"details": [
{
"module":"abc:1.2.3"
},
{
"module":"def:2.3.4"
},
{
"module":"ghi:4.5.6"
}
]
},
{
"modules":set of modules here
"details": [
{
"module":"jkl:7.8.9"
},
{
"module":"mno:10.11.12"
},
{
"module":"pqr:13.14.15"
}
]
}
]
What I have tried and is working is :
rootArray.forEach((entry)=> {
let line = entry.details.map((detailEntry)=>{
//merge detailEntry into single line
})
entry.details = line
});
My question is : is this a right approach or is there any better solution available ?
Two maps is probably the right approach. It will return a new array rather than mutating the original array.
map over the main data array, and then assign the result of mapping over details to the details property of the object that you're returning on each iteration.
const data=[{modules:"set of modules here",details:[{name:"abc",version:"1.2.3"},{name:"def",version:"2.3.4"},{name:"ghi",version:"4.5.6"}]},{modules:"set of modules here",details:[{name:"jkl",version:"7.8.9"},{name:"mno",version:"10.11.12"},{name:"pqr",version:"13.14.15"}]}];
const out = data.map(obj => {
return {
modules: obj.modules,
details: obj.details.map(detail => {
const { name, version } = detail;
return { module: `${name}:${version}` };
})
};
});
console.log(out);
Additional documentation
Destructuring assignment
map
Template/string literals
const data = [
{
"modules": "set of modules here",
"details": [
{
"name":"abc",
"version":"1.2.3"
},
{
"name":"def",
"version":"2.3.4"
},
{
"name":"ghi",
"version":"4.5.6"
}
]
},
{
"modules": "set of modules here",
"details": [
{
"name":"jkl",
"version":"7.8.9"
},
{
"name":"mno",
"version":"10.11.12"
},
{
"name":"pqr",
"version":"13.14.15"
}
]
}
]
const result = data.map( item => ({modules: item.modules, details: item.details.map(i => {
return { module: `${i["name"]}:${i["version"]}` }
})}))
console.log(result)
your approach seems correct.
there is another way to achieve the expected structure, making the copy of the initial array however instead of modifying the existing.
const result = rootArray.map(({ details, ...rest }) => ({
details: details.map(/*transform*/),
...rest
));
How can I get the names of different activity in an array by using map function in this type of response. So that in a new array, assume that activity[] i will get names of all the activities mention below.
if the array is
const response = [
{
"Groups" : {
"Roles" : {
"submission": {
"subject" : {
"name": "history",
}
}
}
}
}
];
I managed to do this using an IIFE but there may be cleaner ways
assuming there in one object in the array and no other path to other permission
const response = [
{
"Roles" : {
"Permission" : {
"PERMISSION1": {
"Activity" : {
"name": "Manage Clients",
}
},
"PERMISSION2": {
"Activity" : {
"name": "Manage Users",
}
}
}
}
}
];
let activities = (() => {
let res = []
for (let perm in response[0].Roles.Permission) {
for (let act in response[0].Roles.Permission[perm]) {
res.push(response[0].Roles.Permission[perm][act].name)
}
}
return res})()
console.log(activities)
At first, you should convert Permission object to array, cause object doesn't have method map.
Then you could use map function where you can collect all your permissions' names for every item in response
const response = [{
"Roles": {
"Permission": {
"PERMISSION1": {
"Activity": {
"name": "Manage Clients",
}
},
"PERMISSION2": {
"Activity": {
"name": "Manage Users",
}
}
}
}
}];
response.forEach((item) => {
item.Activities = Object.values(item.Roles.Permission).map((permission) => permission.Activity.name)
});
alert(JSON.stringify(response));
The only array you have is response. If each item in response has a Roles that has a Permission that has several keys with objects that have Activity with name then you can do the following:
var response = [
{
Roles: {
Permission: {
PERMISSION1: {
Activity: {
name: 'Manage Clients',
},
},
PERMISSION2: {
Activity: {
name: 'Manage Users',
},
},
},
},
},
];
console.log(
response.map(
(item) =>
Object.values(item.Roles.Permission)
.map(
(permission) => permission.Activity.name
)
)
);
I recommend using a flatMap, so use .reduce.
const response = [{
"Roles": {
"Permission": {
"PERMISSION1": {
"Activity": {
"name": "Manage Clients",
}
},
"PERMISSION2": {
"Activity": {
"name": "Manage Users",
}
}
}
}
}];
const activityNames = response.reduce(function (acc, res) {
const permissions = res.Roles.Permission;
const permissionKeys = Object.keys(permissions);
const names = permissionKeys.map(function(permissionKey) {
return permissions[permissionKey].Activity.name;
});
acc.push(...names);
return acc;
}, [])
console.log(activityNames); // ["Manage Clients", "Manage Users"]
I have something like this:
data = [
{
DateMeasured:"2018-08-27T04:46:25",
Steps:100
},
{
DateMeasured:"2018-08-27T04:46:25",
Steps:500
},
{
DateMeasured:"2018-08-27T04:46:25",
Steps:800
},
{
DateMeasured:"2018-08-26T04:46:25",
Steps:400
},
{
DateMeasured:"2018-08-26T04:46:25",
Steps:300
},
{
DateMeasured:"2018-08-25T04:46:25",
Steps:100
}
];
I have an object of data like above, now I want to recreate object with discrict dates but its highest steps, but now i want like this:
data = [
{
DateMeasured:"2018-08-27T04:46:25",
Steps:800
},
{
DateMeasured:"2018-08-26T04:46:25",
Steps:400
},
{
DateMeasured:"2018-08-25T04:46:25",
Steps:100
}
];
How can I achieve this goal?
You could reduce the array by checking the last inserted object with the same date and if not found, insert the object, otherwise check the value and update the array with a greater Step property.
var data = [{ DateMeasured: "2018-08-27T04:46:25", Steps: 100 }, { DateMeasured: "2018-08-27T04:46:25", Steps: 500 }, { DateMeasured: "2018-08-27T04:46:25", Steps: 800 }, { DateMeasured: "2018-08-26T04:46:25", Steps: 400 }, { DateMeasured: "2018-08-26T04:46:25", Steps: 300 }, { DateMeasured: "2018-08-25T04:46:25", Steps: 100 }],
result = data.reduce((r, o) => {
var index = r.findIndex(({ DateMeasured }) => DateMeasured === o.DateMeasured);
if (index === -1) {
r.push(o);
return r;
}
if (r[index].Steps < o.Steps) {
r[index] = o;
}
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you need to sort by the number of steps and take the first 3 elements:
const data = [ { DateMeasured:"2018-08-27T04:46:25", Steps:100 }, { DateMeasured:"2018-08-27T04:46:25", Steps:500 }, { DateMeasured:"2018-08-27T04:46:25", Steps:800 }, { DateMeasured:"2018-08-26T04:46:25", Steps:400 }, { DateMeasured:"2018-08-26T04:46:25", Steps:300 }, { DateMeasured:"2018-08-25T04:46:25", Steps:100 } ];
const sorted = data.sort((a, b) => b.Steps - a.Steps)
const takeFirst3 = sorted.slice(0, 3)
console.log(takeFirst3)
I figure I shouldn't be having trouble with this, but I am. I am trying to switch up the syntax/variables of a JSON object to match a certain parameters.
Here is the JSON I am working with:
{
"name":"BHPhotovideo",
"prices":[
{
"price":"799.00",
"createdAt":"2017-07-23T16:17:11.000Z",
"updatedAt":"2017-07-23T17:21:41.000Z"
},
{
"price":"770.00",
"createdAt":"2017-07-21T16:17:11.000Z",
"updatedAt":"2017-07-23T16:17:11.000Z"
},
{
"price":"599.00",
"createdAt":"2017-07-19T16:17:11.000Z",
"updatedAt":"2017-07-22T16:17:11.000Z"
},
{
"price":"920.00",
"createdAt":"2017-07-22T16:17:11.000Z",
"updatedAt":"2017-07-22T16:17:11.000Z"
}
]
},
etc...
I am just trying to get the data to be formatted like this:
{
"label":"BHPhotoVideo", // Same as name
"data":[
{
"x":"2017-07-23T16:17:11.000Z", // Same as createdAt
"y":799 // Same as price
},
{
"x":"2017-07-21T16:17:11.000Z",
"y":770
},
{
"x":"2017-07-19T16:17:11.000Z",
"y":599
},
{
"x":"2017-07-22T16:17:11.000Z",
"y":920
}
]
},
etc...
The amount of these objects are dynamic/subject to change, I've been making a mess out of foreach loops and trying to piece this together. I keep coming into errors, what's the best way to approach this?
What about this ?
data.map(
(item) => ({
"label":"BHPhotoVideo", // Same as name
"data": item.prices.map(nested => ( {
"x":nested.createdAt,
"y":nested.price
}))
})
)
Did you want the y values to be integers?
var ar = [
{
"name":"BHPhotovideo",
"prices":[
{
"price":"799.00",
"createdAt":"2017-07-23T16:17:11.000Z",
"updatedAt":"2017-07-23T17:21:41.000Z"
},
{
"price":"770.00",
"createdAt":"2017-07-21T16:17:11.000Z",
"updatedAt":"2017-07-23T16:17:11.000Z"
},
{
"price":"599.00",
"createdAt":"2017-07-19T16:17:11.000Z",
"updatedAt":"2017-07-22T16:17:11.000Z"
},
{
"price":"920.00",
"createdAt":"2017-07-22T16:17:11.000Z",
"updatedAt":"2017-07-22T16:17:11.000Z"
}
]
},
{
"name":"Adorama",
"prices":[
{
"price":"799.00",
"createdAt":"2017-07-22T16:17:11.000Z",
"updatedAt":"2017-07-23T17:21:41.000Z"
},
{
"price":"799.00",
"createdAt":"2017-07-20T16:17:11.000Z",
"updatedAt":"2017-07-23T16:17:11.000Z"
},
{
"price":"810.00",
"createdAt":"2017-07-18T16:17:11.000Z",
"updatedAt":"2017-07-22T16:17:11.000Z"
},
{
"price":"799.00",
"createdAt":"2017-07-17T16:17:11.000Z",
"updatedAt":"2017-07-22T16:17:11.000Z"
}
]
}
];
var out = ar.map( function(a) {
return {
"label" : a.name,
"prices" : a.prices.map( function(aa) { return {x: aa.createdAt, y: aa.price} })
}
});
console.log( out );
map over the original array returning a changed object; returning the name, and a new array from using map over the prices.
const obj2 = obj.map((item) => {
return {
label: item.name,
data: item.prices.map((data) => {
return {
x: data.createdAt,
y: data.price
}
})
}
});
DEMO
I use an api which returns an object 'results'. The 'results' object holds other objects which have type article or detail.
Each one of the the result (article or detail) contain their own object results with different pages of that type.
What i want to do: store the results in an array 'pages' where i order them by all the articles first then all the details
I want to sort them in the array because I need to display the articles first then the detail pages in the frontend.
var pages = [];
_.find(results, function(r) {
if(r.type === 'article') {
_.forEach(r.results, function(p) {
p.type = 'article';
pages.push(r);
});
} else if(r.app === 'detail') {
_.forEach(r.results, function(p) {
p.type = 'detail';
pages.push(p);
});
}
});
This was my attempt with Lodash find but in the frontend i still have results where the details are displayed after the articles.
Note: The api result can be like this: {detail}, {article} but also like this {detail}, {article}, {detail}, {article}
Sample data:
results: [
{
type: 'article',
results: {
{
title: '',
body: '',
...
},
...
}
},
{
type: 'detail',
results: {
{
title: '',
body: '',
...
},
...
}
},
...
]
After a long dicussion :
var sorted;
var articles = [];
var details = [];
var source = [{
app: "article",
results: [
{ title: "a" },
{ title: "b" },
{ title: "c" }
]
}, {
app: "detail",
results: [
{ title: "d" },
{ title: "e" },
{ title: "f" }
]
}, {
app: "article",
results: [
{ title: "g" },
{ title: "h" },
{ title: "i" }
]
}];
for (var i = 0; i < source.length; i++) {
switch (source[i].app) {
case "article": articles = articles.concat(source[i].results); break;
case "detail": details = details.concat(source[i].results); break;
}
}
sorted = articles.concat(details);
console.log("articles =", JSON.stringify(articles));
console.log("details =", JSON.stringify(details));
console.log("sorted =", JSON.stringify(sorted));
console.log("source =", JSON.stringify(source));