array replaces instead of concatenating values [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this array declared as
rowData = []
I want to update this array with a forloop but it keeps replacing the values instead of updating it. I guess I'm doing something wrong.
rowDataDefs(){
for (let to of this.detailedOrders){
console.debug(to)
this.rowData = [
{table_no: to.table.tableNo, order_no: to.orderNo, date_time: to.table.createdDate }
]
}
}

rowDataDefs(){
for (let to of this.detailedOrders){
console.debug(to)
this.rowData.push(
{table_no: to.table.tableNo, order_no: to.orderNo, date_time: to.table.createdDate });
}
}

You need to use array.push() instead of replacing the same index(0th index) value every time.
rowDataDefs(){
for (let to of this.detailedOrders){
console.debug(to)
this.rowData.push(
{
table_no: to.table.tableNo,
order_no: to.orderNo,
date_time: to.table.createdDate
}
)
}
return this.rowData;
}

Related

Changing array by map() in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I want to change array with map() but it doesn't work.
Here is my code.
const colors = ["#9abc9c", "#9ecc71", "#9498db", "#9b59b6", "#967e22", "#974c3c", "#91c40f"];
colors.map(color => color[1]="1");
Input what i want is change every first number after "#" into 1.
colors = ["#1abc9c", "#1ecc71", "#1498db", "#1b59b6", "#167e22", "#174c3c", "#11c40f"];
I'd be so appreciate it if you let me solve this problem.
SOLUTION 1
If you want to replace first character after # with 1 then you can do as:
const colors = [
'#9abc9c',
'#9ecc71',
'#9498db',
'#9b59b6',
'#967e22',
'#974c3c',
'#91c40f',
];
const result = colors.map((color) => color.replace(/\d/, '1'));
console.log(result);
SOULTION 2
You should always pick first solution but for another solution you can think below solution also.
const colors = [
'#9abc9c',
'#9ecc71',
'#9498db',
'#9b59b6',
'#967e22',
'#974c3c',
'#91c40f',
];
const result = colors.map((color) => `#1${color.split('').slice(2).join('')}`);
console.log(result);

JavaScript filter method not giving unique result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
i am trying to filter the unique ones from a given array using filter method but its throwing an empty array, here is the code
function dual(a) {
if (Array.isArray(a)) {
let unique = a.filter((val, index) => {
a.indexOf(val) == index
})
return unique;
}
}
console.log(dual([3, 1, 1, 2, 2])) //expected result [3,1,2] but showing []
You may use Set() to create a unique array:
const arr = [3,1,1,2,2]
var uniqueArray = [...new Set(arr)]
console.log(uniqueArray)
You are missing return statements from both your function and your filter array method.

Why is my answer in the compiler not the same, with the answer from leetcode compiler? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple code, its essence is to remove duplicate numbers from a sheet. But on my server, the data received does not match the data on leetcode. For what reason could this be?
var deleteDuplicates = (head) => {
for(let i = 0; i < head.length; i++) {
for(let a = i + 1; a < head.length; a++) {
if(head[i] === head[a] ) {
head.splice([a], 1)
}
}
}
return head;
};
console.log(deleteDuplicates([1,1,2]));
Output: [1,2]
Leetcode:
Your input
[1,1,2]
Output
[1,1,2]
Expected
[1,2]
How about using a Set? It's simpler and you'll avoid modifying a list that you're iterating over.
var deleteDuplicates = (head) => [...new Set(head)];

Javascript if statement: Which pair of characters in this code is optional? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've was given this is a multiple choice JS question. I said [], I just wanted to check if this is correct and if not why?
Consider this if statement:
if (loggedIn) {
body_classes = ["user-active"];
}
Which pair of characters in this code is optional?
""
()
{}
[]
The optional characters are the braces {}
if (loggedIn) {
body_classes = ["user-active"];
}
is the same as
if (loggedIn)
body_classes = ["user-active"];
If you remove the square brackets then body_classes becomes a string rather than an array.

Simple javascript for selecting random element out of array outputting "undefined" on 2 elements (jsfiddle included) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Below is my code, for some reason it outputs undefined for ['6'] & ['7'], all the other ones work. I don't understand what is going wrong.
var array = [
['1'],
['2'],
['3'],
['4'],
['5'],
['6']
['7'],
['8'],
['9'],
['10']
];
if(document.getElementById("random-element")) {
var rand = array[Math.floor(Math.random() * array.length)];
document.getElementById('random-element').innerHTML = rand;
}
https://jsfiddle.net/ggky7a03/
You missed a comma in your array, which will explain why those 2 values are not returning (they don't exist)
Side note, array is a reserved word in JS, so you can't (shouldn't) use it for your variable name, so change it to
var myAwesomeArray = [
// or similar
Here's your fixed code

Categories