1644418140
Làm thế nào bạn có thể tìm thấy tổng các phần tử của nó? Giải pháp là một phương thức array.reduce (). Hàm Array.prototype.reduce () có thể được sử dụng để lặp qua một mảng, thêm giá trị phần tử hiện tại vào tổng các giá trị mục trước đó.
Để tìm mảng tổng trong Javascript của hai số, hãy sử dụng phương thức array.reduce (). Phương thức Reduce () giảm một mảng thành một giá trị duy nhất. Hàm Reduce () thực thi hàm được cung cấp cho mỗi giá trị mảng (từ trái sang phải). Giá trị trả về của một phương thức được lưu trữ trong bộ tích lũy (kết quả / tổng số).
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
Tổng đối số là bắt buộc . Nó là giá trị ban đầu hoặc giá trị trả về trước đó của hàm.
CurrentValue là đối số bắt buộc . Nó là giá trị của phần tử hiện tại trong một mảng.
CurrentIndex là một đối số tùy chọn . Nó là chỉ số của phần tử hiện tại.
Arr là một đối số tùy chọn. Nó là đối tượng mảng mà phần tử hiện tại thuộc về.
Giá trị ban đầu là một đối số tùy chọn. Nó là một giá trị được truyền cho hàm dưới dạng giá trị ban đầu.
Lần đầu tiên gọi lại được gọi là tổng và giá trị currentValue có thể là một trong hai giá trị. Ví dụ: nếu một giá trị ban đầu được cung cấp trong lệnh gọi phương thức Reduce (), tổng giá trị sẽ bằng giá trị ban đầu và giá trị currentValue sẽ tương tự với giá trị đầu tiên trong mảng.
Nếu không có giá trị ban đầu nào được cung cấp, tổng giá trị sẽ bằng với mục đầu tiên trong mảng và giá trị currentValue sẽ tương tự với mục thứ hai.
Hãy xác định một mảng có năm giá trị và sau đó tìm tổng của mảng bằng phương thức array.reduce ().
// app.js
let data = [11, 21, 46, 19, 18];
sum = data.reduce((a, b) => {
return a + b;
});
console.log('The sum is: ', sum);
The sum is: 115
Đó là nó. Chúng tôi đi đến tổng của tất cả các số nguyên của mảng.
Những gì nó thực hiện đằng sau cảnh là trong trường hợp đầu tiên, giá trị ban đầu là 0 và phần tử đầu tiên là 11. Vì vậy, 11 + 0 = 11.
Trong vòng lặp thứ hai, giá trị cũ của chúng tôi là 11 và giá trị tiếp theo là 21. Vì vậy, 11 + 21 = 32. Trong chu kỳ tiếp theo, giá trị cũ của chúng tôi là 32 và giá trị tiếp theo là 46. Vì vậy, 46 + 32 = 78.
Trong chu kỳ thứ ba, giá trị cũ của chúng ta là 78 và giá trị mới là 19. Vì vậy, 78 + 19 = 97.
Trong vòng lặp cuối cùng, giá trị cũ của chúng ta là 97 và giá trị tiếp theo là 18, do đó 97 + 18 = 115.
Vì vậy, đây là cách nó tính tổng tất cả các phần tử của mảng.
Trong ví dụ này, chúng tôi chưa xác định một giá trị ban đầu, nhưng bạn có thể xác định một giá trị ban đầu và nó sẽ lấy làm giá trị cũ đầu tiên và sau đó nó sẽ bắt đầu thêm các giá trị tiếp theo trong mảng.
Đó là nó cho hướng dẫn này.
1622207074
Who invented JavaScript, how it works, as we have given information about Programming language in our previous article ( What is PHP ), but today we will talk about what is JavaScript, why JavaScript is used The Answers to all such questions and much other information about JavaScript, you are going to get here today. Hope this information will work for you.
JavaScript language was invented by Brendan Eich in 1995. JavaScript is inspired by Java Programming Language. The first name of JavaScript was Mocha which was named by Marc Andreessen, Marc Andreessen is the founder of Netscape and in the same year Mocha was renamed LiveScript, and later in December 1995, it was renamed JavaScript which is still in trend.
JavaScript is a client-side scripting language used with HTML (Hypertext Markup Language). JavaScript is an Interpreted / Oriented language called JS in programming language JavaScript code can be run on any normal web browser. To run the code of JavaScript, we have to enable JavaScript of Web Browser. But some web browsers already have JavaScript enabled.
Today almost all websites are using it as web technology, mind is that there is maximum scope in JavaScript in the coming time, so if you want to become a programmer, then you can be very beneficial to learn JavaScript.
In JavaScript, ‘document.write‘ is used to represent a string on a browser.
<script type="text/javascript">
document.write("Hello World!");
</script>
<script type="text/javascript">
//single line comment
/* document.write("Hello"); */
</script>
#javascript #javascript code #javascript hello world #what is javascript #who invented javascript
1616670795
It is said that a digital resource a business has must be interactive in nature, so the website or the business app should be interactive. How do you make the app interactive? With the use of JavaScript.
Does your business need an interactive website or app?
Hire Dedicated JavaScript Developer from WebClues Infotech as the developer we offer is highly skilled and expert in what they do. Our developers are collaborative in nature and work with complete transparency with the customers.
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
Get your business app with JavaScript
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
#hire dedicated javascript developers #hire javascript developers #top javascript developers for hire #hire javascript developer #hire a freelancer for javascript developer #hire the best javascript developers
1589255577
As a JavaScript developer of any level, you need to understand its foundational concepts and some of the new ideas that help us developing code. In this article, we are going to review 16 basic concepts. So without further ado, let’s get to it.
#javascript-interview #javascript-development #javascript-fundamental #javascript #javascript-tips
1658766120
Xin chào các bạn hôm nay tôi muốn hướng dẫn các bạn cách tìm số lần xuất hiện của phần tử trong một mảng trong javascript và cả chỉ mục của chúng.
Hãy bắt đầu…
Mã số -
let array = [6,1,4,4,2,8,3,4,4,4,5,10,5,9,11,
6,1,4,4,2,8,3,4,4,4,5,10,5,9,11,6,1,4,4,2,8,3,4,4,4,5,10,5,9,11];
let target = 4const occurences = (array,target) => {
let result = 0
let index = []
for (let i = 1; i <= array.length + 1; i++) {
if(array[i] === target){
result++
index.push(i)
}
else{
continue
}
}
return `${target} occured ${result.length} times at indexes - ${index}`
}console.log(occurences(array,target))
Đầu ra -
4 occured 15 times at indexes - 2,3,7,8,9,17,18,22,23,24,32,33,37,38,39
Liên kết: https://medium.com/faun/finding-occurences-of-element-in-array-in-js-58c5fb418379
#javascript
1626321063
PixelCrayons: Our JavaScript web development service offers you a feature-packed & dynamic web application that effectively caters to your business challenges and provide you the best RoI. Our JavaScript web development company works on all major frameworks & libraries like Angular, React, Nodejs, Vue.js, to name a few.
With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.
Looking for professional JavaScript web app development services? We provide custom JavaScript development services applying latest version frameworks and libraries to propel businesses to the next level. Our well-defined and manageable JS development processes are balanced between cost, time and quality along with clear communication.
Our JavaScript development companies offers you strict NDA, 100% money back guarantee and agile/DevOps approach.
#javascript development company #javascript development services #javascript web development #javascript development #javascript web development services #javascript web development company