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);
Related
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.
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 11 months ago.
Improve this question
I've got an array:
test = [{'id':'73','name':'bar', 'contact': [{'name':'barFoo','tel':'3333'}]},{'id':'45','name':'foo', 'contact':[]}]
I try to get contact.tel in first object with 'id':'73'
I'm using find method :
let contactTel = test.find(x => x.id === '73').contact.tel;
but it dosen't work. What I'm doing wrong ?
it should be like this
const test = [{'id':'73','name':'bar', 'contact': [{'name':'barFoo','tel':'3333'}]},{'id':'45','name':'foo', 'contact':[]}]
let contactTel = test.find(x => x.id === '73').contact[0].tel;
console.log(contactTel)
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 want to filter an array of files by their extensions.
I have written following code:
const exts = ['.log, ts']
const files = [
'a.ts', 'b.xml', 'c.log'
]
const res = files.filter(f => {
return exts.some(e => {
return f.endsWith(e)
})
})
console.log(res)
In my opinion, it should output['a.ts', 'c.log'].
But I get an empty array.
I am looking at this since hours. I don't get it. Please help me.. What's wrong ?
You need more than one string in the array for exts
const exts = ['.log', '.ts']
const
exts = ['.log', '.ts'],
files = ['a.ts', 'b.xml', 'c.log'],
res = files.filter(f => exts.some(e => f.endsWith(e)));
console.log(res);
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;
}
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