A general chat about JavaScript. Ask anything you like or share your best cat picture. Please check Google/StackOverflow for answers before asking for help. And remember, be nice! Don't ask to ask, just ask
const person = {
id: 1,
firstName: 'john',
lastName: 'doe',
age: 25,
};
const json = JSON.stringify(person);
console.log(json); // Should return: { id: 1, age: 25 }
// output is: {"id":1,"firstName":"john","lastName":"doe","age":25}
const { id, age } = person; const filteredPerson = { id, age };
const { id, firstName, lastName, age } = person
i get only value
1 John Doe 25
id:1 firstName: john lastName: Doe, age:25
using only destructuring?
I'm trying to remove every other occurrence of a word from an array. I have made an indexArray, which lists index numbers of a given word as it appears. I was hoping to use this to create a filter on the original array.
let story =
'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';
let storyWords = story.split(" ");
function listIndex(arr, val) {
const indexArray = [];
let idx = arr.indexOf(val);
while (idx != -1) {
indexArray.push(idx);
idx = arr.indexOf(val, idx + 1);
}
console.log(indexArray);
// You have an array. Measure length, at every even index number store the value (the initial index of the word)
for(let i = 0; i < indexArray.length; i++) {
if(i % 2 === 1){
console.log(indexArray[i])
}
}
}
listIndex(storyWords, "really");
This is the path I'm on currently, but could be barking up the wrong tree. Please could you give me tips on a good way forward?
@Lauro235 I expect that your listIndex
function will correctly produce all indices of the given word. I would suggest calling it listIndices
because it returns multiple indices.
If you want to remove words from the storyWords
array by index, the way forward is to go... backwards. :-) By removing the last word first, you ensure that the indices of all words before it stay the same.
For fun and education, here is a functional way to write listIndices
using Underscore:
const equalTo = expected => actual => actual === expected;
function listIndices(array, word) {
return _.chain(array)
.pairs()
.groupBy(_.compose(equalTo(word), _.property('1')))
.get('true')
.map('0')
.value();
}
Good point on the naming of the function. I haven't seen this chaining of methods that often (starting from a new line each time), but this is fine because Javascript doesn't recognise white space in this instance?
Looking forward to googling these methods and figuring out what you did :P
This language is confusing me....
"The this keyword references the calling object which provides access to the calling object’s properties. In the example above, the calling object is goat and by using this we’re accessing the goat object itself, and then the dietType property of goat by using property dot notation".
It's taken from a lesson about the this keyword as it relates to an object. https://www.codecademy.com/courses/introduction-to-javascript/lessons/advanced-objects/exercises/adv-this
const goat = {
dietType: "herbivore",
makeSound() {
console.log("baaa");
},
diet() {
console.log(this.dietType); //REVIEW
},
};
goat.diet()
My point of confusion is on what the calling object is. Do they mean goat.diet() or do they just mean the object it self const goat?
@Lauro235
Good point on the naming of the function. I haven't seen this chaining of methods that often (starting from a new line each time), but this is fine because Javascript doesn't recognise white space in this instance?
Right. In principle, statements are terminated by semicolons. Sometimes when you write a linebreak without a semicolon, the JS interpreter is still able to figure out that you meant to terminate the statement, but in principle, line breaks do not have a meaning in JavaScript. In fact, neither does indentation. We say that whitespace is insignificant in JS.
I entered "fetch" there and ended up at https://developer.mozilla.org/en-US/docs/Web/API/fetch. On that page, it says:
cache
A string indicating how the request will interact with the browser's HTTP cache. The possible values,default
,no-store
,reload
,no-cache
,force-cache
, andonly-if-cached
, are documented in the article for the cache property of the Request object.
cache
property is probably what you need.
@jgonggrijp
window.open("https://www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
WindowProxy
, which you might be able to read the URL on, depending on the same-origin policy of the website that you're opening in the popup. Documentation on MDN: Window.open, WindowProxy, same-origin policies.
add some readability to this line.
const groupBy = (dataArray, getGroupingKey) => dataArray.reduce(
(reducedHash, arrayElement) => ((reducedHash[getGroupingKey(arrayElement)] ||= []).push(arrayElement), reducedHash),
{}
);
now, replace those: ||= [], (..., ...)
const groupBy = (dataArray, getGroupingKey) => dataArray.reduce(
(reducedHash, arrayElement) => {
const key = getGroupingKey(arrayElement);
let group = reducedHash[key];
if (!group) {
group = [];
reducedHash[key] = group;
}
group.push(arrayElement);
return reducedHash;
},
{}
);