Convert string into an object using reduce javascript - javascript

I am trying to convert a string like "sheep" into an object like this:
{
"s":{
"initial":1,
"final":1
},
"h":{
"initial":1,
"final":1
},
"e":{
"initial":2,
"final":2
},
"p":{
"initial":1,
"final":1
}
}
Currently I can use reduce method in javascript and achive this:
const names = 'sheep'.split('');
const count = (names) =>
names.reduce((acc, name) => ({ ...acc, [name]: (acc[name] || 0) + 1 }), {});
console.log(count(names)) //{ s: 1, h: 1, e: 2, p: 1 }
I have tried to read similar posts but I am pretty new to JS. Can anyone please help me? Thanks.

Try like this
const names = "sheep".split("");
const count = (names) =>
names.reduce(
(acc, name) => ({
...acc,
[name]: {
initial: (acc?.[name]?.initial ?? 0) + 1,
final: (acc?.[name]?.final ?? 0) + 1,
},
}),
{}
);
console.log(count(names));

This works printing your desired output but I'm not sure why you want the initial and final counts to be the same
const letters = 'sheep'.split('');
const count = (letters) =>
letters.reduce((obj, letter) => ({ ...obj, [letter]: {initial: (obj[letter]?.initial || 0) + 1, final: (obj[letter]?.final || 0) + 1} }), {});
console.log(count(letters))
Alternatively, you could expand a function there using braces and do some if's in order to have the code more readable instead of the ternaries

Related

Create an Dynamic Array Object with key value pairs

I need to create an Dynamic key value pairs from the existing object
const balanceScheule = 1255;
const reqCost = [{Labour Cost: "1555"}, {Material Cost: "1575"}]; // key is dynamic and keeps on changing
const amfqtyCost = 1416;
Here the logic is to create an new array of object and subtract the amfqtyCost from reqCost
Logic i Have written
reqCost.forEach(element => {
const adjustedAmount = Object.entries(element).map((m) => {
let adjustedAmount = parseInt(m[1]) - amfqtyCost;
return adjustedAmount;
});
// console.log(...adjustedAmount)
});
this return 139 and 159 which is (1555 - 1416 = 139) and (1575 1416 = 159) respectively
Expected output :
[{Labour Cost: "139"}, {Material Cost: "159"}]
How to do i merge ?
You just need to return the updated object from within map function. Also for the outer iteration use map instead of forEach to return the final result
const balanceScheule = 1255;
const reqCost = [{
'Labour Cost': "1555",
}, {
'Material Cost': "1575",
}]; // key is dynamic and keeps on changing
const amfqtyCost = 1416;
const updatedData = reqCost.map(element => {
return Object.assign({}, ...Object.entries(element).map(([key, value]) => {
let adjustedAmount = parseInt(value) - amfqtyCost;
return {
[key]: String(adjustedAmount)
};
}));
});
console.log(updatedData);
You can do something like this:
const reqCost = [{
'Labour Cost': "1555"
}, {
'Material Cost': "1575"
}];
const amfqtyCost = 1416;
const adjustedCost = reqCost.map(cost => ({
[Object.keys(cost)[0]]: (parseInt(Object.values(cost)[0]) - amfqtyCost).toFixed(0)
}));
console.log(adjustedCost);
// OR, if you prefer to be a bit more verbose:
const adjustedCost2 = reqCost.map(cost => {
const [key, value] = Object.entries(cost)[0];
return {
[key]: (parseInt(value) - amfqtyCost).toFixed(0)
}
});
console.log(adjustedCost2);
You can reverse the Object.entries
{ key : value } => [ [ key, value ] ]
transformation by using Object.fromEntries
[ [ key, value ] ] => { key : value }
the code will look like this
reqCost.map((obj) =>
Object.fromEntries(
Object.entries(obj).map(([key, value]) => [
key,
parseInt(value) - amfqtyCost,
])
)
);

curr is undefined in reduce on array of objects

As an input, I have an array of objects , each one of these objects has a number of properties,
I would like to have an output that is also an array of objects with the same properties unchanged except for one property that I like to be changed into an accumulative value.
input example :
let input_array = [{ a:'a',b:'b',c: 1},{a:'d',b:'e',c:2},{a:'g',b:'h',c: 3}];
output example
let output_array = [{ a:'a',b:'b',c: 1},{a:'d',b:'e',c:3},{a:'g',b:'h',c: 6}];
Here 's what I tried :
let output_array = [];
input_array.reduce((acc, curr) => {
output_array.push({
a: curr.a,
b: curr.b,
c : acc.c + curr.c
})
},0);
I keep getting c NaN and acc undefined .
In this case I'd prefer to .map one array to the other, and keep a variable with the accumulated c:
const input = [{ a:'a',b:'b',c: 1},{a:'d',b:'e',c:2},{a:'g',b:'h',c: 3}];
const output = input.map(((acc) => ({ c, ...rest }) => ({ ...rest, c: acc += c }))(0));
console.log(output);
For sure your solution also works if you actually return the new accumulator from the reducer:
const input = [{ a:'a',b:'b',c: 1},{a:'d',b:'e',c:2},{a:'g',b:'h',c: 3}];
const output = [];
input.reduce((acc, curr) => {
acc = acc + curr.c;
output.push({ ...curr, c: acc });
return acc;
},0);
console.log(output);

mapping one object to another using some functional programming

I would like to map one array of object into another in a more functional style, I am using typescript.
Basically I am using delete to remove a property on a object, I would like to know if there is a better way to write it.
const data = props.data.map(d => ({
order: d.position,
logs: d.batches.map(b => {
let log= {
amount: b.scrap,
batchNumber: '', // NO GOOD
}
if (!b.batch || b.batch.length === 0) {
delete log.batchNumber // NO GOOD
}
return log
}),
}))
example input data:
const data = [
position: 1,
batches: [
{batchNumber: '', ammount: 3}
]
]
result:
const data = [{
order: 1,
logs:[ {ammount:3}]
}
]
You can do another map on the batches to return a new array of objects, and attach that to your returned object instead:
const out = data.map(({ position: order, batches }) => {
const logs = batches.map(({ batchNumber, ammount }) => {
if (batchNumber) return { batchNumber, ammount };
return { ammount };
});
return { order, logs }
});
DEMO
One approach would be to make a shallow copy of the target omitting keys you want to delete, for example:
let drop = key => obj => Object.keys(obj).reduce((r, k) =>
k === key ? r : {...r, [k]: obj[k]}, {});
let test = [
{foo:11, bar:2, baz: 3},
{foo:22, bar:2, baz: 3},
{foo:33, bar:2, baz: 3},
];
console.log(test.map(drop('bar')));
To add another option to the mix: it is possible to use Object.assign to optionally assign the property:
const data = [{
position: 1,
batches: [{batchNumber: '',ammount: 3}, {batchNumber: 'withNr',ammount: 4}]
}];
const res = data.map(d =>
({
order: d.position,
logs : d.batches.map(({ammount, batchNumber}) => Object.assign({ammount}, batchNumber ? {batchNumber} : null ))
})
);
console.log(res);

How to use if statement within map in javascript/vuejs

I'm building a small application in Vuejs where I'm getting a response data and I'm mapping it to a variable, I've got few elements which has empty array, so while mapping I want to check the condition and map accordingly. Here is my code:
this.model = a.map(i => Object.assign({
'id': i.id,
'meeting_date': i.schedule,
'meeting_call': i.type,
'event_type': i.event_type,
'venue': i.venue,
'with_client': i.with_client
},{
if(i.meeting.meeting_summaries)
{
'meeting_summaries': i.meeting_summaries.map(ms => ({
client_name: ms.client_name,
nature: ms.nature,
action: ms.action,
mention: ms.user_id,
feedback: ms.feedback
}))
}
},
map is purely functional, it doesn't modify the elements instead return a newly formed array, so you can do like this:
this.model = a.map(i => {
var item = {}
item['id']= i.id,
item['meeting_date']= i.schedule,
item['meeting_call']= i.type,
item['event_type']= i.event_type,
item['venue']= i.venue,
item['with_client']= i.with_client
if(i.meeting && i.meeting.meeting_summaries) {
item['meeting_summaries']= i.meeting.meeting_summaries.map(ms =>({
client_name: ms.client_name,
nature: ms.nature,
action: ms.action,
mention: ms.user_id,
feedback: ms.feedback
}))
}else {
item['meeting_summaries'] = []
}
return item
}
In your case you can just swap to ternary expression:
this.model = a.map(i => Object.assign({
'id': i.id,
'meeting_date': i.schedule,
'meeting_call': i.type,
'event_type': i.event_type,
'venue': i.venue,
'with_client': i.with_client
}, (i.meeting.meeting_summaries) ? { // if condition is met
'meeting_summaries': i.meeting_summaries.map(ms => ({
client_name: ms.client_name,
nature: ms.nature,
action: ms.action,
mention: ms.user_id,
feedback: ms.feedback
}))
} : {} // otherwise don't do anything
The idea is the following:
const a = [1, 2, 3, 4, 5];
const b = a.map(number => Object.assign({a: number},
(number > 2) ? {b: ++number} : {}, // add 'b' for numbers > 2
(number % 2 === 0) ? {c: number + ' is even'} : {} // add 'c' for even numbers
))
console.log(b)

How to update Map inside a List

If I have a immutable list like:
List([
Map({ something: 1 }),
Map({ something: 2 }),
])
How can I do set something = 5 where something = 1?
You can use map + set in order to achieve that
console.clear();
const list = Immutable.List([
Immutable.Map({ something: 1 }),
Immutable.Map({ something: 2 }),
Immutable.Map({ something: 4 }),
Immutable.Map({ something: 1 })
])
const newList = list.map(item =>
item.get("something") === 1 ? item.set("something", 5) : item
);
console.log('Old',JSON.stringify(list.toArray(),null, ""));
console.log('New',JSON.stringify(newList.toArray(),null, ""));
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.2/immutable.min.js"></script>
#Qop's answer will work great if you are looking for a specific key.
If you want to update multiple values in each Map, you could use .mapEntries() inside the list.map call.
things
.map(thing => {
return thing.mapEntries(([ k, v ]) => {
const val = (v === 5) ? 1 : v;
return [ k, val ]
})
});
here is a link to working plunk

Categories

Resources