Get name and value of url params [duplicate] - javascript

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 3 years ago.
I have some url like this
/posts/singlepost?page=99
I need to get key and value of query in url and to put in new array, that i can loop,and use that key and value in my http request. In url page can be something else, i dont know what can be

You can do something like that:
// Get params.
const params = Array.from(new URLSearchParams(location.search))
// Add a loop.
params.forEach(([key, value]) => /* key is 'page' and value is 99 */)

You can use split() by ? and then split() the second element by =
let str = '/posts/singlepost?page=99';
let res = str.split('?')[1].split('=');
console.log(res)

Related

Javascript treats variable as string [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 1 year ago.
I have the following state change operation:
const handleQuantity=(e)=>{
const targetedProductId=e.target.id
// console.log(targetedProductId)
const targetedProductQuantity=e.target.value
setOrderProductQuantity({
...orderProductQuantity,
targetedProductId:targetedProductQuantity
})
}
and my state is this :
const [orderProductQuantity,setOrderProductQuantity]=React.useState({})
The
console.log(targetedProductId)
shows the correct value
It always turns out that orderProductQuantity is updated with something like this:
{"targetedProductId":e.target.value} instead of {e.target.id:e.target.value}. Could you please explain why this happens, how could I fix it ? Thank you !
P.S. Tried with string literals, also doesn't work
The problem is that you're not attributing the id value as the signatures of the parameter.
The correct way of doing so would be:
const handleQuantity = (e) => {
const targetedProductId = e.target.id;
const targetedProductQuantity = e.target.value;
setOrderProductQuantity({
...orderProductQuantity,
[targetedProductId]: targetedProductQuantity
});
}
With the square brackets you're telling javascript that you want to map the value in targetedProductId, instead of actually writing targetedProductId

Get "id" part of outlook emai [duplicate]

This question already has answers here:
JavaScript - Get Portion of URL Path
(6 answers)
Closed 1 year ago.
Just a quick question, how would you go about getting the "id" from this URL:
https://outlook.live.com/mail/0/inbox/id/AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D
These URLs change alot, but i just want the message id.
Its not just a query param, so i can't just go and get it from there.
How would i do this?
You can just split the URL by / and get the 8th index, and if there is parameters, just split it by ? and get the first index
const url =
'https://outlook.live.com/mail/0/inbox/id/AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D?a=1&'
let url_split = url.split('/')
let id_with_parameter = url_split[7]
let id = id_with_parameter.split('?')[0]
console.log(id)
Output AQQkADAwATM0MDAAMS0xZGUwLTNjMTAtMDACLTAwCgAQAB%2FnQ1lgT6dDlqIakp3j4qk%3D
This probably has a lot of limitation and not the best way

how to select an index that has a specific name without case sensitivity? [duplicate]

This question already has answers here:
Access JavaScript property case-insensitively?
(19 answers)
Closed 2 years ago.
and I want to choose the index where the table is case insensitive
let table = {'tAble': [{'number': 1}],'CHair': [{'number': 2}]}
console.log(table[/table/i])
You can do this using Object.keys
table[Object.keys(table).find(x=> /table/i.test(x))]
Here you're getting an array of all keys, and then using find to test them with your RegExp, and then using the found key to retrieve the correct property of the object.
let index = Object.keys(table).findIndex(key => key.toLowerCase() === 'table');
let table = {'tAble': [{'number': 1}],'CHair': [{'number': 2}]}
let key = (Object.keys(table).find(i=>i.toLowerCase() === 'table'));
console.log(key)
console.log(table[key])

Extracting id value from a facebook url [duplicate]

This question already has answers here:
How to get URL parameter using jQuery or plain JavaScript?
(34 answers)
Closed 5 years ago.
I want a JavaScript regex to extract the value of an id from facebook profile URL. For example, I have a url
https://www.facebook.com/profile.php?id=100004774025067
and I just want to extract the value after the id= which is the number 100004774025067 part only.
But In some cases I will need the user profile url from a post. For example a user posted something, and if i get the profile url link of the post then I get the link like this:
https://www.facebook.com/profile.php?id=100001883994837&hc_ref=ART7eWRecFS8mMIio66GdaH378zlJMXzisnKubh5PtgINeVwTfOil5aBIyff71OamWA
As you can see, after the value of id there's an additional parameter specified.
I only need to get the id value, no matter what else is in the link.
You can use the string as a URL and extract the id parameter using searchParams:
Without additional parameters:
var fb_url = "https://www.facebook.com/profile.php?id=100004774025067";
var url = new URL(fb_url);
var uid = url.searchParams.get("id");
console.log(uid);
With more parameters:
var fb_url = "https://www.facebook.com/profile.php?id=100004774025067&hc_ref=ART7eW";
var url = new URL(fb_url);
var uid = url.searchParams.get("id");
console.log(uid);
You can do it in the following way
function getNumber(str){
console.log(str.match(/(?:id=)(\d+)/)[1]);
return str.match(/(?:id=)(\d+)/)[1];
}
getNumber('https://www.facebook.com/profile.php?id=100004774025067');
getNumber('https://www.facebook.com/profile.php?id=100001883994837&hc_ref=ART7eWRecFS8mMIio66GdaH378zlJMXzisnKubh5PtgINeVwTfOil5aBIyff71OamWA ');
which matches any number after id

Accessing dynamic content with numbers [duplicate]

This question already has answers here:
read name of unknown properties
(2 answers)
Closed 8 years ago.
I'm working with wikipedia API and have a problem with the results I'm getting.
{"query":{
"pages":{
"48636":{
"pageid":48636,
I don't know what the ID is going to be when I make the call, how do I access the 48636?
if gon.activateWiki
$(gon.keywords).each (index, element) ->
console.log element.name
$.getJSON( 'http://sv.wikipedia.org/w/api.php?action=query&format=json&prop=revisions%7Clinks%7Cimages%7Ccategories&rvprop=content&titles=' + element.name + '&callback=?' , (data) ->
console.log data.query.pages
)
You want the trusty old Object.keys
firstKey = Object.keys(data.query.pages).shift()
lastKey = Object.keys(data.query.pages).pop()
nthKey = Object.keys(data.query.pages)[n-1]
firstPage = data.query.pages[firstKey]
lastPage = data.query.pages[lastKey]
nthPage = data.query.pages[n-1]
You can access it like an array:
console.log(data.query.pages["48636"]);
Update:
To get values from unknown property's of object you need to use for..in statement. Or you can use slice method and transform your object to array:
var pages = Array.prototype.slice.call(data.query.pages);

Categories

Resources