I have object after CSV conversion in this shape:
const data = {
"assets/0/description": "propertyImage",
"assets/0/filename": "a31989e07277954d93bca65e2d02dcb9.jpeg?p=cc_vo_small",
"assets/0/mimetype": "jpeg?p=cc_vo_small",
"assets/0/ordinal": "0",
"assets/0/url": "/usados/2022/09/04/0/a31989e07277954d93bca65e2d02dcb9.jpeg?p=cc_vo_small",
"assets/1/description": "propertyImage",
"assets/1/filename": "3bc3a9a4982508941ea178cca2ad31f1.jpeg?p=cc_vo_small",
"assets/1/mimetype": "jpeg?p=cc_vo_small",
"assets/1/ordinal": "1",
"assets/1/url": "",
combustion: "diesel",
createdAt: "2022-09-14T13:42:55.807Z",
engine: "235 CV",
firstDateRegistration: "2021",
gearbox: "automática secuencial",
"location/city": "Vitoria-Gasteiz",
"location/country": "España",
"location/distributor": "",
"location/houseNumber": "",
"location/postalCode": "",
"location/province": "Alava",
"location/street": "C/ Portal de Betoño, 13-15",
make: "volvo",
"metadata/0/key": "Año",
"metadata/0/value": "2021",
mileage: "29000",
model: "v90 cross country b5 pro awd aut.",
"optionals/0": "",
originalColorString: "gris",
"price/currency": "EUR",
"price/financialPrice": "53700",
"price/price": "56999",
saleDate: "Wed Sep 14 2022",
seller_type: "professional",
traction: "",
version: "V90 Cross Country B5 Pro Awd Aut."
};
Im trying to convert nested items that csv made flat into the correct format:
assets/0/description -> {
assets: [
{
description: "propertyImage",
filename: "...",
...
}
]
}
probably number between is an array and just / is an object. I tried to build reduce function and create it recursively but it looks more complicated than this. Any chance that there is a library for it? Or has someone already met this problem in the past?
Related
I have this following json nested structure that i need to construct in pentaho:
{
"num_matricula": "4242424",
"limit_date": "2018-01-01",
"admission_date": "2018-01-01",
"cost_center": "anywhere",
"pos_number": "pos-test",
"role": "b63e065f-d7e0-49e1-91b7-88f74516e3fe",
"department": "bf559996-a8b9-4f5e-af57-86111b0dbde3",
"pagamento": {
"vinculo": "clt",
"valor": "4200",
"recorrencia": "mensalista",
"contaBancaria": {
"banco": "001",
"carta": "c9160763-db6c-4e8c-a1ad-ad8709c99be2"
}
},
"deficiencia": false,
"jornada": "De segunda a sexta das 15 as 19",
"profile": {
"name": "John Doe",
"email": "john.doe#acessodigital.com.br",
"mobile": "11911111111"
},
"exame": {
"clinica": "6dc84ce4-7d9f-48ec-b9b1-a8a895a21fd4",
"data": "2018-01-01",
"hora": "14:00",
"obs": "Comparecer de manhã",
"guia": "e37dab24-c7a4-4b92-b9d1-32ed538b8300",
},
"docs": ["c9e26093-5e0c-4bd2-bea3-ac5182a6179f"],
"send_sms": true,
"send_email": true
}
All the values come from the step "Select Values" which one gather the columns and the values to construct the json structure how it is showed in the following screenshot:
I've seen some solutions about using JavaScript to construct nested json structure in Pentaho, but i was not so sure about to create this using values from other steps. Can someone help me to construct this in pentaho? Pleasee.
In my React Native application, I am accessing data from my store in the following form:
Array [
Checkout {
"date": 2020-12-27T13:24:08.734Z,
"id": "Sun Dec 27 2020 08:24:08 GMT-0500 (EST)",
"items": Array [
Object {
"productBrand": "Microsoft",
"productCategory": "Gaming",
"productId": "p1",
"productTitle": "Xbox",
"quantity": 2,
"x": 1.815,
},
Object {
"productBrand": "Apple",
"productCategory": "Computers",
"productId": "p2",
"productTitle": "MacBook Pro",
"quantity": 1,
"x": 1.905,
},
],
"total": 3.720,
},
Checkout {
"date": 2020-12-27T13:24:47.790Z,
"id": "Sun Dec 27 2020 08:24:47 GMT-0500 (EST)",
"items": Array [
Object {
"productBrand": "Apple",
"productCategory": "Computers",
"productId": "p2",
"productTitle": "MacBook Pro",
"quantity": 1,
"x": 1.905,
},
],
"total": 1.905,
},
]
I am trying to use VictoryPie to create a pie chart that shows productBrand weighted by the sum of x over all the objects. In this example, I would need a pie chart showing Microsoft and Apple, weighted by 1.815 and 2*1.905 = 3.81, respectively. Is there any way to do this without writing a separate function to calculate these sums? I would like the pie chart to update automatically every time new data is added to the store.
I tried this, where history is a variable containing the above array but no pie chart is produced.
<VictoryPie data={history} x={(data) => data.items.productBrand} y={(data) => data.items.x} />
See my working sample: https://codesandbox.io/s/react-victory-pie-chart-forked-kpe39?file=/src/index.js
Like this:
x="productBrand"
y={(data) => data.x * data.quantity}
For anyone trying to do something similar, I ended up extracting the data I needed by using a nested for loop within the useSelector hook:
const allBrands = useSelector(state => {
let allData = {};
for (const key1 in state.history.history) {
for (const key2 in state.history.history[key1].items) {
if (allData.hasOwnProperty(state.history.history[key1].items[key2].productBrand)) {
allData[state.history.history[key1].items[key2].productBrand] += state.history.history[key1].items[key2].x;
} else {
allData[state.history.history[key1].items[key2].productBrand] = state.history.history[key1].items[key2].x;
}
}
};
let dataArray = [];
for (const prop in allData) {
dataArray.push({ brand: prop, total: allData[prop] })
}
return dataArray
});
Passing allBrands to the VictoryPie data prop produced the correct pie chart.
I have elasticsearch database as following:
{
"market": "NEW YORK",
"hour_time_stamp": "2019-08-24 01:00:00",
"model_year": 2014,
"num_devices": 28
},
{
"market": "COLUMBIA",
"hour_time_stamp": "2019-08-12 01:00:00",
"model_year": 2019,
"num_devices": 2
},
{
"market": null,
"hour_time_stamp": "2019-08-11 01:00:00",
"model_year": 2016,
"num_devices": 1
},
{
"hour_time_stamp": "2019-08-02 01:00:00",
"model_year": 2014,
"num_devices": 750
},
{
"market": "COLUMBIA",
"hour_time_stamp": "2019-07-12 01:00:00",
"model_year": 2018,
"num_devices": 21
}
As you can see, some values of market are null and in some cases the market field is missing. I have written the following elastic.js query to fetch the market and perform aggregation on it:
let queryBody = ejs.Request()
.size(0)
.query(
ejs.BoolQuery()
.must(
ejs.RangeQuery('hour_time_stamp').gte(this.lastDeviceDate).lte(this.lastDeviceDate)
)
)
.agg(ejs.TermsAggregation('market_agg').field('market').order('sum', 'desc').size(50000)
.agg(ejs.SumAggregation('sum').field('num_devices'))
);
The data that I need is market name and sum of num_devices for that market(since market can be repeated). The query I have written returns only the market where market value is defined. I need to include the missing market as well as "market": null in my bucket. Any ideas how I can do that?
I'm putting together a very simple fictional sneaker app, using the MERN stack.
I wouldn't call myself a newbie, but I'm not an expert either. I was able to create the backend just fine and generate a json rest-api. My problem is on the front-end. I'm simply trying to console.log a property from an object.
For example, I have a route that gets an array of sneaker objects. In each object, it contains info such as the Sneaker Model, Colorway, Year the sneaker was released, here a preview of the object.
{
"_id": "5c5dff42489e28576c28cd9e",
"model": "Air Jordan 11",
"colorway": "Win Like 82",
"year": 1997,
"text": "Cool kicks",
"mainimage": "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664062/xjwkpbqwl1pmawpocygx.jpg",
"subimage_1": "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664062/y8h1biytup4rgxqrm2qe.jpg",
"subimage_2": "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664063/wrwska4tnog4vselcwp7.jpg",
"subimage_3": "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664063/h9dyki3gdgfjcccchhia.jpg",
"subimage_4": "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664064/qwrsabomnsqk7pjyx5ss.jpg",
"user": "5c4b4635c9de365620cd797b",
"likes": [],
"comments": [],
"date": "2019-02-08T22:14:26.127Z",
"__v": 0
}
I use destructuring to get the sneaker data from my application (Redux) state:
const { sneakers } = this.props.sneaker;
When I try to console.log the array of objects using console.log(sneakers) it works as expected these are the results:
(3) [{…}, {…}, {…}]
0:
colorway: "Win Like 82"
comments: []
date: "2019-02-08T22:14:26.127Z"
likes: []
mainimage: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664062/xjwkpbqwl1pmawpocygx.jpg"
model: "Air Jordan 11"
subimage_1: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664062/y8h1biytup4rgxqrm2qe.jpg"
subimage_2: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664063/wrwska4tnog4vselcwp7.jpg"
subimage_3: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664063/h9dyki3gdgfjcccchhia.jpg"
subimage_4: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549664064/qwrsabomnsqk7pjyx5ss.jpg"
text: "Cool kicks"
user: "5c4b4635c9de365620cd797b"
year: 1997
__v: 0
_id: "5c5dff42489e28576c28cd9e"
__proto__: Object
1:
colorway: "taxi"
comments: []
date: "2019-02-08T19:20:59.467Z"
likes: []
mainimage: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653655/gag2aktdhyfru5s5dnni.jpg"
model: "Air Jordan 12"
subimage_1: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653656/wcg9jmo84cl8mhxb2iwl.jpg"
subimage_2: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653656/mxistpujhh0xikzqwcu0.jpg"
subimage_3: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653657/dcp1y2jrqz2nkgyhuwfr.jpg"
subimage_4: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653657/dyee9uzb7aj1dp46rlnj.jpg"
text: "the holy grails"
user: "5c4b4635c9de365620cd797b"
year: 1997
__v: 0
_id: "5c5dd69beaef7140e0d2926d"
__proto__: Object
2:
colorway: "Obsidian"
comments: []
date: "2019-02-03T23:01:21.937Z"
likes: []
mainimage: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549234878/yzdoot1cf3jnbq31ndkg.jpg"
model: "Air Jordan 12"
subimage_1: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549234879/jbjw8hngvieyvnizfb8b.jpg"
subimage_2: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549234880/qu1gguxpfp2mapd5ivo2.jpg"
subimage_3: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549234880/gcntdjlznjjbxifsmufl.jpg"
subimage_4: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549234881/ck9emeurmaqleizaqe1e.jpg"
text: "Awesome kicks"
user: "5c4b4553c9de365620cd797a"
year: 1997
__v: 0
_id: "5c5772c10ffd0f2e789999b2"
__proto__: Object
length: 3
__proto__: Array(0)
If I want to say for instance just the second sneaker I use:
console.log(sneakers[1])
I get what I expected:
{_id: "5c5dd69beaef7140e0d2926d", model: "Air Jordan 12", colorway: "taxi", year: 1997, text: "the holy grails", …}
colorway: "taxi"
comments: []
date: "2019-02-08T19:20:59.467Z"
likes: []
mainimage: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653655/gag2aktdhyfru5s5dnni.jpg"
model: "Air Jordan 12"
subimage_1: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653656/wcg9jmo84cl8mhxb2iwl.jpg"
subimage_2: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653656/mxistpujhh0xikzqwcu0.jpg"
subimage_3: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653657/dcp1y2jrqz2nkgyhuwfr.jpg"
subimage_4: "http://res.cloudinary.com/dwgjvssdt/image/upload/v1549653657/dyee9uzb7aj1dp46rlnj.jpg"
text: "the holy grails"
user: "5c4b4635c9de365620cd797b"
year: 1997
__v: 0
_id: "5c5dd69beaef7140e0d2926d"
__proto__: Object
However, when I try to get a property from just one sneaker, let's say for example the colorway from the second sneaker I use:
console.log(sneakers[1].colorway)
And I run into this error TypeError: Cannot read property 'colorway' of undefined.
I'm a little hard pressed to figure out why. I figured since I can log data from one sneaker if I wanted to I should be able to log just one property from that sneaker. Obviously, there is something that I'm not doing quite right, but I can't figure out what.
Most likely, the data object prints as a browser-level convenience but is not actually ready for property access. To circumvent this problem, I've used this pattern with success:
const SomeComponent = ({sneakers}) => {
return (
<div>
{
sneakers && sneakers.map(sneaker => {
return (
<div>{sneaker.colorway}</div>
)
})
}
</div>
)
}
export default SomeComponent;
I have a javascript object that is coming from Source1 and I am unable to change its native structure or naming convention. I am using this data to feed into a 3rd party plugin to generate some chart data. This plugin however is using the key names as the identifiers on the chart and they are not descriptive or clear enough.
I am trying to run the object through a conversion function where it will change all of the key names to their defined equivalent.
Here is an example of what I am trying to do:
var obj = [{
SubmissionID: "28935",
MetaCreatedDate: "12 Mar 2018",
Program: "Brand Risk Management",
ViewedByInvestigator: "No",
},
{
SubmissionID: "28936",
MetaCreatedDate: "12 Mar 2018",
Program: "Brand Risk Management",
ViewedByInvestigator: "Yes",
}]
function convertNames(obj){
// Converted names
var map = [{
SubmissionID: 'SubmissionIdentifier',
MetaCreatedDate: 'CreationDate',
Program: 'ProgramName',
ViewedByInvestigator: 'Viewed'
}];
// Loop through the object and convert all key names to their equivalent
for(var prop in obj){
// Convert Here
}
return obj;
}
Desired Output:
[{
SubmissionIdentifier: "28935",
CreationDate: "12 Mar 2018",
ProgramName: "Brand Risk Management",
Viewed: "No",
},
{
SubmissionIdentifier: "28936",
CreationDate: "12 Mar 2018",
ProgramName: "Brand Risk Management",
Viewed: "Yes",
}]
https://jsfiddle.net/hbg4sfqh/7/
I'd combine the .map array method and a function to convert your key names to get the result you want. To convert the key names, you'll want to use bracket notation, so something like: newObj[keyMap[oldKey]] = oldObj[oldKey] should work.
Here's a simple implementation for your example:
const obj = [{
SubmissionID: "28935",
MetaCreatedDate: "12 Mar 2018",
Program: "Brand Risk Management",
ViewedByInvestigator: "No",
}, {
SubmissionID: "28936",
MetaCreatedDate: "12 Mar 2018",
Program: "Brand Risk Management",
ViewedByInvestigator: "Yes",
}];
const nameMap = {
SubmissionID: 'SubmissionIdentifier',
MetaCreatedDate: 'CreationDate',
Program: 'ProgramName',
ViewedByInvestigator: 'Viewed'
}
function renameKeys(obj, map) {
const newObj = {};
for (let key in obj) {
newObj[map[key]] = obj[key];
}
return newObj;
}
console.log(obj.map(item => renameKeys(item, nameMap)));
I'd also note that if you happen to be using the lodash library, you can also use it's _.mapKeys method to do this.
I'm gonna use .map() function to change the key names. The input data will remain unchanged. Hope this helps.
var obj = [{
SubmissionID: "28935",
MetaCreatedDate: "12 Mar 2018",
Program: "Brand Risk Management",
ViewedByInvestigator: "No",
},
{
SubmissionID: "28936",
MetaCreatedDate: "12 Mar 2018",
Program: "Brand Risk Management",
ViewedByInvestigator: "Yes",
}]
var output = obj.map(element => ({
SubmissionIdentifier: element.SubmissionID,
CreationDate: element.MetaCreatedDate,
ProgramName: element.Program,
Viewed: element.ViewedByInvestigator
}));