Cannot find object properties in an Javascript array [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 am currently learning JavaScript and I am having a horrible time with objects in an array.
I'm trying to find them. It won't find.
I'm trying display them, they are not displaying correctly.
I've been trying to figure this out for about 2 days and I'm lost
Here is the code
const notes = [{
title: 'The big trip',
body: ' The next big trip will be back to thailand'
},
{
title: 'Fitness goals',
body: 'really enjoying the ab programme '
},
{
title: 'life decisions',
body: 'the move overseas '
}
]
console.log(notes.length)
const findNote = function(notes, noteTitle) {
const index = notes.findIndex(function(note, index) {
return note.title === noteTitle
})
return notes[index]
}
const note = findNote(notes, 'Fitness Goals')
console.log(notes)

Your code is ok. Please check your function arguments here:
const note = findNote(notes, 'Fitness Goals')
There is no any note with title 'Fitness Goals'. Change this string to:
const note = findNote(notes, 'Fitness goals')
'Goals' and 'goals' are different strings in Javascript, so program can't find any note

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);

concatenate string with prop value received in react/Javascript in a component [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
how to concatenate a string with a variable's value in javascript (more specifically react).In this ToolsCard component, I am getting the title as prop and I want to make a string 'download title which is being received as prop, how should I do this.I tried the below way but I am getting an error, Is there some alternative to do this?
const ToolsCard = ({title}) => {
const str='download'+{title}
}
const ToolsCard = title => 'download ' + title;
I don't know what else to tell you. You might want to export the ToolsCard function maybe. And you should definitely read some Javascript and React tutorials.

Cannot read properties of undefined (reading 'items') [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 other JSON data similar to this, hence, I would like to filter it.
const data = [{
"1": {items: { Part1: true, Part2: true } },
id: "zaMR9TR7hNV3p3VFNumyNbXMto93",
displayName: name1
}]
However, I keep getting the error of
Cannot read properties of undefined (reading 'items')
const filter = data.filter(
(a) =>
a.["1"].items.Part1 == true
);
How do I fix this?
you have an extra "." right before ["1"]
a["1"].items.Part1
By the way, if i were you, i would rename items by "item", because it's not an array, just an object.
You are accessing elemet incorrectly. please use this
const filter = data.filter(
(a) =>
a["1"].items.Part1 == true
);
error was here, the extra dot(.) after a => a.["1"], it should be a["1"]

Javascript Array.filter() and Array.some() [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 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);

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