I'm trying to get the full results of this code execution, Right now what I get is only the date's of all values, but I need it so that all columns are displayed, so name and date. Can anyone help?
const result = [
{ id: 1, name: 'Tom', date: '2022-05-17T22:00:00.000Z' },
{ id: 2, name: 'Joe', date: '2022-05-12T22:00:00.000Z' },
{ id: 3, name: 'Frederiko', date: '2022-05-23T22:00:00.000Z' },
{ id: 4, name: 'John', date: null },
{ id: 5, name: 'Boer', date: '2022-05-23T22:00:00.000Z' }
]
let time = dayjs().format('YYYY-MM-DD')
let eyy = result.filter(item1 => !result.find(item2 => item1.name == item2.name && dayjs(item2.date).format('YYYY-MM-DD') == time))
console.log(eyy);
<script src="https://unpkg.com/dayjs#1.8.21/dayjs.min.js"></script>
The result.filter method can be simplified by removing the use of result.find, for example...
const result = [
{ id: 1, name: 'Tom', date: '2022-05-17T22:00:00.000Z' },
{ id: 2, name: 'Joe', date: '2022-05-12T22:00:00.000Z' },
{ id: 3, name: 'Frederiko', date: '2022-05-23T22:00:00.000Z' },
{ id: 4, name: 'John', date: null },
{ id: 5, name: 'Boer', date: '2022-05-23T22:00:00.000Z' }
]
let time = dayjs().format('YYYY-MM-DD')
let eyy = result.filter(item => dayjs(item.date).format('YYYY-MM-DD') === time)
console.log(eyy);
<script src="https://unpkg.com/dayjs#1.8.21/dayjs.min.js"></script>
It's probably worth noting that depending where you are in the world the above will most likely not return anything because there are no dates matching today's date (May 24th).
From your comments it appears you want to modify the structure of the filtered results. To do this you can use the .map() method...
const result = [
{ id: 1, name: 'Tom', date: '2022-05-17T22:00:00.000Z' },
{ id: 2, name: 'Joe', date: '2022-05-12T22:00:00.000Z' },
{ id: 3, name: 'Frederiko', date: '2022-05-23T22:00:00.000Z' },
{ id: 4, name: 'John', date: null },
{ id: 5, name: 'Boer', date: '2022-05-24T22:00:00.000Z' }
];
let time = dayjs().format('YYYY-MM-DD');
let eyy = result.map(item => {
return {
name: item.name,
date: dayjs(item.date).format('YYYY-MM-DD')
};
});
eyy = eyy.filter(item => item.date === time);
console.log(eyy);
<script src="https://unpkg.com/dayjs#1.8.21/dayjs.min.js"></script>
Related
states = [{
name: telangana,
cities: [{
id: 1,
name: foo
}, {
id: 2,
name: joo
}, {
id: 3,
name: goo
}]
},
{
name: punjab,
cities: [{
id: 4,
name: tyu
}, {
id: 5,
name: ery
}, {
id: 6,
name: doo
}]
},
{
name: mumbai,
cities: [{
id: 7,
name: eee
}, {
id: 8,
name: qqq
}, {
id: 9,
name: www
}]
},
]
I want response like [foo, joo, goo, tyu, ery,doo, eee,qqq,www]
Can someone help me ?
Just write one line:
Learn more about reduce() and map()
const states = [{ name: "telangana", cities: [{ id: 1, name: "foo" }, { id: 2, name: "joo" }, { id: 3, name: "goo" }] }, { name: "punjab", cities: [{ id: 4, name: "tyu" }, { id: 5, name: "ery" }, { id: 6, name: "doo" }] }, { name: "mumbai", cities: [{ id: 7, name: "eee" }, { id: 8, name: "qqq" }, { id: 9, name: "www" }] }, ];
const result = states.reduce((acc, { cities }) => [...acc, ...cities.map(({ name }) => name)], []);
console.log(result);
const getNames = (data) => {
const nameArr = [];
data.forEach((ele) => {
ele.cities.forEach((ele2) => {
nameArr.push(ele2.name);
})
})
return nameArr;
}
getNames(states);
Try this please!
states = [{
name: "telangana",
cities: [{
id: 1,
name: "foo"
}, {
id: 2,
name: "joo"
}, {
id: 3,
name: "goo"
}]
},
{
name: "punjab",
cities: [{
id: 4,
name: "tyu"
}, {
id: 5,
name: "ery"
}, {
id: 6,
name: "doo"
}]
},
{
name: "mumbai",
cities: [{
id: 7,
name: "eee"
}, {
id: 8,
name: "qqq"
}, {
id: 9,
name: "www"
}]
},
]
const wantedArray = []
for(i=0; i < states.length; i++){
for(j=0; j < states[i].cities.length; j++){
wantedArray.push(states[i].cities[j].name)
}
}
console.log(wantedArray)
Just give it an empty array, then you loop through the states indexes, each index in states will have a cities array, then you just need to loop it again in that array to get each name of the cities. From then, you are using the push method that Javascript provides to push it to the empty array.
Here's how I'm doing it in JSFiddle, there will have a better way to do this, too.
I have 2 arrays of objects returning from 2 different fetch
const result1 = [
{
name: 'matteo',
age: 20,
id: 1,
},
{
name: 'luca',
age: 24,
id: 2,
},
];
const result2 = [
{
warnings: 'yes',
hobby: "tennis",
id: 1,
},
{
warnings: 'many',
hobby: "ping pong",
id: 2,
},
];
This is my current approach but it will merge the entire object from result2 to result1 if they have the same id
const t = result2.reduce((acc, curr) => {
acc[curr.id] = curr;
return acc;
}, {});
const d = result1.map((d) =>
Object.assign(d, t[d.id])
);
The current result is:
{
name: 'matteo',
age: 20,
id: 1,
warnings: "yes",
hobby: "tennis"
},
{
name: 'luca',
age: 24,
id: 2,
warnings: "many",
hobby: "ping pong"
},
I would like to move only the warnings prop from the second array of objects into the first array of objects where the id of object is equal
Desired output:
const result3 = [
{
name: 'matteo',
age: 20,
id: 1,
warnings: "yes"
},
{
name: 'luca',
age: 24,
id: 2,
warnings: "many"
},
];
You can use map to create a new array, and find to get any warning with a matching id:
let result1 = [
{ id: 1, name: 'a', age: 1 },
{ id: 2, name: 'b', age: 2 },
{ id: 3, name: 'c', age: 3 },
{ id: 4, name: 'd', age: 4 }
];
let result2 = [
{ id: 1, hobby: 'aa', warnings: 'aaa' },
{ id: 2, hobby: 'bb', warnings: 'bbb' },
{ id: 4, hobby: 'dd', warnings: 'ddd' }
];
let includeWarnings = (data, warningsArr) => data.map(obj => {
// `w` will be undefined if no matching warning is found
let w = warningsArr.find(warn => warn.id === obj.id);
// Return all the data in `obj`, and the "warnings" property
// of `w` if `w` is defined
return { ...obj, ...(w ? { warnings: w.warnings } : {}) };
});
console.log(includeWarnings(result1, result2));
Note you'd potentially be better off if your data format was structured with id mappings in mind:
let result1 = {
id1: { name: 'name1', age: 1 },
id2: { name: 'name2', age: 2 },
.
.
.
}
let result2 = {
id1: { hobby: 'hobby1', warnings: 'warnings1' },
id2: { hobby: 'hobby2', warnings: 'warnings2' },
.
.
.
}
This question already has answers here:
How to sort an array of objects by date?
(4 answers)
Closed 2 years ago.
I have an array :
const arr = [
{ name: 'abc', date: '30/03/2014' },
{ name: 'cde', date: '30/03/2015' },
{ name: 'fgh', date: '20/04/2014' },
{ name: 'xyz', date: '17/09/2014' },
];
How can I sort this array so that the output would be like this:
const arr = [
{ name: 'cde', date: '30/03/2015' },
{ name: 'xyz', date: '17/09/2014' },
{ name: 'fgh', date: '20/04/2014' },
{ name: 'abc', date: '30/03/2014' },
];
// sort the array with date in latest first.
Using Array#sort with your own sorting comperator. For this split the dates and build with taht in the right sequence a new Date which can be compared.
const arr = [
{ name: 'abc', date: '30/03/2014' },
{ name: 'cde', date: '30/03/2015' },
{ name: 'fgh', date: '20/04/2014' },
{ name: 'xyz', date: '17/09/2014' },
];
arr.sort((a,b) => {
let tempA = a.date.split('/');
let tempB = b.date.split('/');
return ((new Date(tempB[2],tempB[1],tempB[0])) - (new Date(tempA[2],tempA[1],tempA[0])));
});
console.log(arr);
Need to merge 2 lists of updates, local and server.
Using redux (but it doesn't really matter) i need to refresh the updates list.
const localUpdates = [
{ id: 1, name: 'one', date: 'old' },
{ id: 2, name: 'two', date: 'old' },
{ id: 3, name: 'three', date: 'old' },
];
const serverUpdates = [
{ id: 1, name: 'one', date: 'new' },
{ id: 4, name: 'four', date: 'new' },
];
Desired output:
updates: [
{ id: 1, name: 'one', date: 'new' },
{ id: 2, name: 'two', date: 'old' },
{ id: 3, name: 'three', date: 'old' },
{ id: 4, name: 'four', date: 'new' },
]
Thanks in advance (having brainfreeze looiking at this for the past hour)
You can make use of Array.prototype.reduce followed by Object.values to the concated array in order to perform an update
const localUpdates = [
{ id: 1, name: 'one', date: 'old' },
{ id: 2, name: 'two', date: 'old' },
{ id: 3, name: 'three', date: 'old' },
];
const serverUpdates = [
{ id: 1, name: 'one', date: 'new' },
{ id: 4, name: 'four', date: 'new' },
];
const concatArr = localUpdates.concat(serverUpdates);
// we will be excepting new values for same id
const resObj = concatArr.reduce((acc, item)=> {
acc[item.id] = {...(acc[item.id] || {}), ...item};
return acc;
}, {})
console.log(Object.values(resObj));
create a temporary object, loop over both arrays and add each object's id in the temporay object as key and whole object as value. Objects with same id's will be overwritten. Finally use Object.values to get the merged objects in an array.
const localUpdates = [
{ id: 1, name: 'one', date: 'old' },
{ id: 2, name: 'two', date: 'old' },
{ id: 3, name: 'three', date: 'old' },
];
const serverUpdates = [
{ id: 1, name: 'one', date: 'new' },
{ id: 4, name: 'four', date: 'new' },
];
const obj = {};
localUpdates.forEach(local => (obj[local.id] = local));
serverUpdates.forEach(server => (obj[server.id] = server));
console.log(Object.values(obj));
.as-console-wrapper { max-height: 100% !important; top: 0; }
I am looking for a way to modify array of objects like this:
[
{
id: 1,
name: 'xyz',
count: 3,
},
{
id: 2,
name: 'aaa',
count: 2,
},
{
id: 6,
name: 'bbb',
count: 1,
},
]
Now I want to map it shomehow to receive new array of objects without count properties but with duplicated objects by its count value. We will have:
[
{
id: 1,
name: 'xyz',
},
{
id: 1,
name: 'xyz',
},
{
id: 1,
name: 'xyz',
},
{
id: 2,
name: 'aaa',
},
{
id: 2,
name: 'aaa',
},
{
id: 6,
name: 'bbb',
},
]
I tried to do it with map and reduce but it didn't work out as expected...
You could use a nested mapping with an outer Array#flatMap.
var data = [{ id: 1, name: 'xyz', count: 3 }, { id: 2, name: 'aaa', count: 2 }, { id: 6, name: 'bbb', count: 1 }],
result = data.flatMap(({ count, ...o }) =>
Array.from({ length: count }, _ => ({ ... o })));
console.log(result);
Nina Scholz solution works fine, if you want something easier to read:
var data = [{
id: 1,
name: 'xyz',
count: 3,
},
{
id: 2,
name: 'aaa',
count: 2,
},
{
id: 6,
name: 'bbb',
count: 1,
},
];
var output = [];
for (var i = 0; i < data.length; i++) {
var element = data[i];
for (var j = 0; j < element.count; j++) {
output.push({
id: element.id,
name: element.name
});
}
}
console.log(output);