Why is my reduce() function in javascript not working? [duplicate] - javascript

This question already has answers here:
JavaScript reduce throwing error for more than two items
(4 answers)
Closed 2 years ago.
arr is an array of customers with different phone plans, but I dont know why my reduce function isn't working to add up my numOfLines. e.g.
arr=[{
customer: {
whatPlan: "no contract",
monthlyCost: 50,
tax:0.4
},
monthlyFee:1,
numOfLines:1
}..........]
console.log(arr.reduce((total, item)=>{return total + item.numOfLines;}));
Can someone help me regarding this? Thanks in advance!

arr.reduce((total, item)=>{return total + item.numOfLines},0);
You need to add "0" to make it work

Related

how to append to list below example given and output also? [duplicate]

This question already has an answer here:
How to merge each object within arrays by index?
(1 answer)
Closed 4 months ago.
This is the data:
data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]
data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1=[]
expected Output:
list1:[{"name":"vikash","roll":39,"hobby":"football","food":"any"},{"name":"kumar","roll":3,"hobby":"basketball","food":"any"}]
Please help me!
Providing both lists are the same length, you're happy there are no key conflicts and you're using Python...
data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]
data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1 = []
for k, v in enumerate(data):
list1.append({**v, **data2[k]})
print(list1)
However in the future please be more concise when asking your questions.
Specify the language
Don't include redundant tags (eg - this question has nothing to do with Django)
Tell us what you've tried!
Also... the "please help me" line isn't required. The whole purpose of this community is to help each other so again this is redundant!
A simpler to understand solution
data=[{"id":1,"name":"vikash","roll":39},{"id":2,"name":"kumar","roll":3}]
data2=[{"hobby":"football","food":"any"},{"hobby":"basketball","food":"any"}]
list1=[]
for i in range(len(data)):
del data[i]['id']
d_main = {}
d_main.update(data[i])
d_main.update(data2[i])
list1.append(d_main)
print(list1)

Calling a nested function with a string (window[]) in JS [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.
You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")

Printing/console logging from map object [duplicate]

This question already has answers here:
How to iterate over Object's property-value pairs?
(8 answers)
Closed 1 year ago.
Lets say I have personalRecords parameter.. and it accepts this map object:
new Map([['100', 9.58], ['200', 19.19]])
I want to take these numbers and print them out / console.log in VSCode in such format:
100m: 9.58s
200m: 19.19s
I can't seem to figure out how to take these numbers out of the object and print them out properly. I appreciate your help in advance.
You can use for each to achieve that
const personalRecord = new Map([['100', 9.58], ['200', 19.19]])
for(let record of personalRecord){
console.log(record[0] + "m: " + record[1] + "s")
}

if runs with low-number >= high-number? [duplicate]

This question already has answers here:
Why is string "11" less than string "3"? [duplicate]
(6 answers)
Closed 3 years ago.
I am making a mistake or something strange is going on but I am not seeing it.
localStorage value = 9
if (localStorage.getItem('TotalProducts') >= '10') {
alert('total products'+localStorage.getItem('TotalProducts'));
}
Why am I receiving the alert?
Alert content is total products9
Thanks everyone for helping.
For others read the comments below there is some really useful information in there.
if (+localStorage.getItem('TotalProducts') >= 10) {
alert('total products'+localStorage.getItem('TotalProducts'));
}
Convert your TotalProducts value of localStorage to a number and then compare it to 10.

Write a function that accepts an array of test scores as an argument then the function should return a number of scores that 80 or higher? [duplicate]

This question already has answers here:
How to filter array values greater than x
(4 answers)
Closed 4 years ago.
I'm still pretty new into JS but I'm having a hard time with this problem. I'm sure the answer is somewhat simple but i can't figure out what to put into the function expression after i've made my array of scores? Do i need to do a for loop?
Thanks
array = [20,30,40,50,60,70,80,92,93,95,98,100]
function testScores(array){
}
you can use array's filter method. please follow below definition.
function testScores(array){
return array.filter(ele=>ele>=80)
}
function testScores(array){
return array.filter(number => number >= 80);
}
This will return an array of scores more 80 and above.
e.g
let scores = [20,30,40,50,60,70,80,92,93,95,98,100];
testScores(scores)
If you just want the count of scores more than 80 you can do
testScores(scores).length

Categories

Resources