How to add special characters like : to array map ? I want to achieve this.
'10': ["11/21/2022", "11/25/2022"]
this is what I have so far
const data = [{
"user_id": "10",
"dates": ["11/21/2022", "11/25/2022"]
}]
const output = data.map(({
user_id,
dates
}) => [user_id, dates]);
console.log(output);
I'm going to guess that you're looking for an array like this as your result:
[
{
10: ["11/21/2022", "11/25/2022"]
},
// ...possibly others here...
]
If so, return a non-array object from your map call rather than an array:
const output = data.map(({ user_id, dates }) => ({[user_id]: dates}));
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^
Example:
const data = [
{
user_id: "10",
dates: ["11/21/2022", "11/25/2022"],
},
];
const output = data.map(({ user_id, dates }) => ({[user_id]: dates}));
console.log(output);
Note how that uses computed property name syntax to create the property in the object, [user_id]: dates. That creates a property using the value of user_id as the name.
If you just want to console.log to appear in the way you described, this then you just have to access the elements from the array and print them in the way you want.
const data = [{
"user_id": "10",
"dates": ["11/21/2022", "11/25/2022"]
}]
const output = data.map(({
user_id,
dates
}) => [user_id, dates]);
console.log(`'${output[0][0]}': ["${output[0][1][0]}, ${output[0][1][1]}"]`);
Related
I want to achieve this format. As you can see on the output there's a bracket but I want to get only the '10': ["11/21/2022", "11/25/2022"] or this one.
{
'10': ["11/21/2022", "11/25/2022"]
}
const data = [{
user_id: "10",
dates: ["11/21/2022", "11/25/2022"],
}, ];
const output = data.map(({
user_id,
dates
}) => ({
[user_id]: dates
}));
console.log(output);
You can only do this if the array has just one entry. Otherwise, how would you know which array element to use?
Then just create it directly, map creates an array, not an object:
const [{ user_id, dates }] = data;
const output = {
[user_id]: dates,
};
Live Example:
const data = [
{
user_id: "10",
dates: ["11/21/2022", "11/25/2022"],
},
];
const [{ user_id, dates }] = data;
const output = {
[user_id]: dates,
};
console.log(output);
Or without the initial destructuring:
const output = {
[data[0].user_id]: data[0].dates,
};
Live Example:
const data = [
{
user_id: "10",
dates: ["11/21/2022", "11/25/2022"],
},
];
const output = {
[data[0].user_id]: data[0].dates,
};
console.log(output);
map is returning array again, so if you wanna get only object you can use [0] to access it directly
sth like this :
console.log(output[0]);
or this :
const output = data.map(({
user_id,
dates
}) => ({
[user_id]: dates
}))[0];
I have an 1D array like this, where a single object inside that array looks this
[
{
date: "01/01/2020",
type1: 264,
type2: 1937,
type3: 30
},
...
]
I want to modify the above array and convert it into a 2D array in the below format
[
[
{
"date": "01/01/2020",
"type": "type1",
"value": 264
},
{
"date": "01/01/2020",
"type": "type2",
"value": 1937
}
.....
],
[
{
"date": "01/01/2020",
"type": "type3",
"value": 30
}
.....
]
]
Basically first array will consist of type1 and type2 values and the second array will consist of type3 value.
I am pretty new to javascript. I have tried this using plain javascript like below
const type1 = data.map((e: any) => ({
date: e.date,
type: "type1",
value: e.type1
}));
const type2 = data.map((e: any) => ({
date: e.date,
type: "type2",
value: e.type2
}));
const type3 = data.map((e: any) => ({
date: e.date,
type: "type3",
value: e.type3
}));
const newData = [type1.concat(type2), type3];
Is there any better way to do it ?
As a first step, a way to convert the object(s) into the split up objects is to specify the property names (this is assuming there are no more unknown properties):
const arr = [{date:'01/01/2020', type1:264,type2: 1937,type3:30}];
const res = _.flatMap(arr,o=> ['type1','type2','type3'].map(t=> ({date:o.date, type:t, value:o[t]})));
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
After that though, I am not certain of the goal, If you want all objects of type 3 in a separate array, regardless of source object, a quick and dirty way could be:
const arr = [{date:'01/01/2020', type1:264,type2: 1937,type3:30}];
const res = [[],[]];
_.flatMap(arr,o=> ['type1','type2','type3'].map(t=> ({date:o.date, type:t, value:o[t]})))
.forEach(o=> res[o.type==='type3' ? 1: 0].push(o));
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
I use React and I have this array in Redux Persist reducer:
const data = [["2020-09-14","15:00","60","Info","April Tucker","Other","yes"],
["2020-09-14","2:00","50","Text","April Tucker","Other","yes"]]
I want to return a new array of objects (for each array in the array) with some of the values from the data array. Example of how it should look like:
let events = [{ title: data[0][3], date: data[0][0] },{ title: data[1][3], date: data[1][0]}]
I tried to use array.map method:
let events [
data.map((index) => ({
title: data[index][3],
date: data[index][0],
})),
]
This does not work as I expected. How to do it properly?
The .map function gives you each item of the array as a parameter in the callback function.
Use it like so:
const data = [
["2020-09-14", "15:00", "60", "Info", "April Tucker", "Other", "yes"],
["2020-09-14", "2:00", "50", "Text", "April Tucker", "Other", "yes"]
]
let events = data.map(item => ({
title: item[3],
date: item[0],
}))
console.log(events)
the callback on the map function takes the actual array item as the first argument and the index as the second, while your function takes index as the first.
Also, you are putting the result of the map operation inside square brackets [ ]. The array produced by map will be placed as the first element in a new array, while you probably don't want that
let events = data.map(arr => ({
title: arr[3],
date: arr[0],
})
With destructuring and shorthand property names it's easy as one two three
const data = [["2020-09-14","15:00","60","Info","April Tucker","Other","yes"],
["2020-09-14","2:00","50","Text","April Tucker","Other","yes"]]
let events = data.map(([date,,,title]) => ({title, date}) )
console.log(events)
I need get data the my Json but I can't use 'key' because the 'key' is different each day.
I tried :
template: function(params) {
const objects = JSON.parse(JSON.stringify(params.data.masterdetail));
for (const obj of objects) {
const keys = Object.keys(obj);
const cont = 0;
keys.forEach(key => {
const valor = obj[key];
console.log('value ', valor[0]);
});
}
I first tried with 0 and then use cont, but with 0 console.log (value is undefined)....
If I use console.log ('value' , valor['name']) IT'S OK ! but I can't use keys and if I use valor[0] is undefined...........
Example Json
{
"headers": [
"headerName": "asdasd",
], //end headers
"datas": [
"idaam": "11",
"idorigen": "11",
"masterdetail": [{
"child1": {
"name": "I AM",
"age": "1"
},
"child2": {
"name": "YOU ARE",
"age": "2"
},
"child3": {
"name": "HE IS",
"age": "3"
},
}] //end masterdetail
]//end datas
}//end JSON
Edit :
I can't use 'keys' because today I receive "name", "typeval" etc. and tomorrow I can get 'surname','id' etc.
If you see in my first img you can see "4" bits of data.
1º obj[key]{
name = "adopt",
typeval= "",
etc
}
2º obj[key]{
"link" = "enlace",
"map" = "map"
etc
}
If I use this code : I get "name" OKEY but
I HAVE PROHIBITED use of value['name'] or value[typeval] because this Json always is dynamic.
var objects = params.data.masterdetail[0];
const keys = Object.keys(objects);
let value;
keys.forEach(key => {
value = objects[key];
console.log(value['name']);
console.log(value['typeval']);
});
I need for example :
var objects = params.data.masterdetail[0];
const keys = Object.keys(objects);
cont = 0 ;
keys.forEach(key => {
value = objects[key];
console.log(value[0]);
});
but value[0] is undefined and then when I arrive 2ºobj[key] link is index 0 but cont maybe is .... 4...
Sorry for my English...
To simply print the objects within the first entry in the masterdetail array, you can do the following:
var objects = params.datas.masterdetail[0];
const keys = Object.keys(objects);
keys.forEach(key => {
console.log('value ', objects[key]);
});
Based on a (suitably corrected - see my comments above) version of the JSON above, this would produce console output as follows:
value {name: "I AM", age: "1"}
value {name: "YOU ARE", age: "2"}
value {name: "HE IS", age: "3"}
Unfortunately it's not 100% clear from the question if this is the output you were looking for, but that's my best guess based on the code.
Your main mistakes were that
1) masterdetail is an array, and all the data is within the first element of that array, so to get the objects within it you need to select that element first. If the array can have multiple elements in real life then you'd need an outer loop around the code above to iterate through it.
2) If you're looping through the keys of an object, you don't need to also iterate through the properties a different way. You seemed to have two loops designed to do the same thing.
I am trying to assemble a string using output from two collections. To do that, I iterate over one of them, using _forEach function, and use it's output as an input for another iterator. But I can't make it work.
Code:
const data1 = [{
label: 'Id',
data: 'id'
},
{
label: 'First name',
data: 'first_name'
},
{
label: 'Last name',
data: 'last_name'
},
{
label: 'IP Address',
data: 'ip_address'
},
];
const data2 = [{
"id": 1,
"first_name": "Robinet",
"last_name": "Golsby",
"ip_address": "201.83.127.236"
}, {
"id": 2,
"first_name": "Kirby",
"last_name": "Feaver",
"ip_address": "143.188.49.149"
}]
let keys = '';
const getKeys = _.forEach(data1, value => keys += '' +value.data);
let stringValue = '';
const getStringValue = _.forEach(tableData, value => stringValue += ' ' + value.first_name);
So I can get a list of keys from the first object, and I can get the list of values from the second, but only if the key is hardcoded. What I want to do is to iterate over each of the entries in data2 in the 'outer' iterator, while providing keys to get the values using iteration over data1. How do I do that?
Edit: the end result would look like:
1 Robinet Golsby 201.83.127.236
seems like:
const result = _.chain(data1)
.map('data') // get keys from data1
.thru(keys => _.map(data2, item => _.at(item, keys))) // iterate data2 and get values by keys
.map(values => _.join(values, ' ')) // values array to string
.value();
result:
["1 Robinet Golsby 201.83.127.236", "2 Kirby Feaver 143.188.49.149"]
I'm not sure if this is exactly what you want, but I think that you can achieve your goal using nesting Array#map calls:
const data1 = [{"label":"Id","data":"id"},{"label":"First name","data":"first_name"},{"label":"Last name","data":"last_name"},{"label":"IP Address","data":"ip_address"}];
const data2 = [{"id":1,"first_name":"Robinet","last_name":"Golsby","ip_address":"201.83.127.236"},{"id":2,"first_name":"Kirby","last_name":"Feaver","ip_address":"143.188.49.149"}];
const result = data2.map((obj) => data1.map(({ label, data }) => ({
label,
data: obj[data]
})));
console.log(result);