String's split method shows undefined [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to get all characters starting from = inside url:
let url = this.$route.query.item
console.log(typeof(url)) // outputs string
let status = url => url.split('=')[1]
it shows split as undefined. What is the problem and how can be it fixed?

Here is what you want:
let url = "foo=bar";
console.log(typeof (url)) // outputs string
let status = url => url.split('=')[1]
console.log(status(url));// "bar"

I think your this.$route.query.item does not contain = character. if you are looking for current url try this may be
let url = this.$router.currentRoute;
console.log(url); // check if this is the url you expect
let status = 'url does not contain = char';
if(url.includes('='){
status = url.split('=')[1];
}
console.log(status);
note: if you actually need to split this.$route.query.item, console log it and check if it contains = or perhaps add a check for it.

Related

How to extract a IP address from JavaScript string data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I have string data aa = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}
in javaScript and I've to extract the exact IP address --10.186.32.137 from this data
I'm trying this command--
b = aa.match(\10.186.32.137\g) but it also matches the pattern like 10.186.32.13. I need to match the exact pattern. Any help to fix this?
One of the most simple regex pattern would be:
const aa = `{"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}`
const reg = new RegExp(/..\....\...\..../g)
const res = aa.match(reg)
console.log(res[0]);
but as posted in comments why not use JSON.parse?
Parse the string with JSON.parse(STRING) then access your network object ("PC-lab-network-452") JSON.parse(aa)["PC-lab-network-452"] then access any valid array index JSON.parse(aa)["PC-lab-network-452"][0] then access the addr property JSON.parse(aa)["PC-lab-network-452"][0].addr
If you want to solve this without regex. Try this :
const a = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]};
Object.keys(a).forEach(item => {
const ipExist = a[item].find(obj => obj.addr === "10.186.32.137");
if (ipExist) {
console.log(ipExist.addr);
}
else {
console.log('IP not found');
}
});

How to check if all elements in a node list have the same class or the same style property value? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I created memory game. The only problem is that when the game is done and the player wins, it doesn't console.log('win)
Code:
let checkingImages = document.querySelectorAll('.card')
checkingImages = Array.from(checkingImages)
let check = checkingImages.every((each)=>{
each.classList.contains('matched')
})
if(check == true){
console.log('win')
}
Inside your every method, you're only checking for the class's existence, not actually returning anything.
You have to write it like this:
let checkingImages = document.querySelectorAll('.card')
checkingImages = Array.from(checkingImages)
let check = checkingImages.every(item => item.classList.contains('matched'))
console.log(check)
Or like this, if you want to stick to your original answer:
let checkingImages = document.querySelectorAll('.card')
checkingImages = Array.from(checkingImages)
let check = checkingImages.every((item) => {
return item.classList.contains('matched')
})
console.log(check)

Javascript: How to parse data from this string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have the following string returned from a server:
{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
I want to extract data:image/png;base64,iVBORw0KGgoAAErkJggg== (the base64DataURL part) from it. What's the best way to do this with Javascript?
Please note that I am asking how to parse values from JSON. This is not the same as this question, which asks how to extract fields from nested objects and arrays.
What I've tried: I have tried
JSON.parse(<string>).href.split('base64DataUrl":"')[1].split('"')[0]
which yields the right answer, but I'm hoping for a more concise solution.
This is JSON, but the part you're interested in isn't exposed as part of the object. So I would suggest that you just consider it a string and use a string-parsing method, like a regular expression.
const string = `{"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}`;
const dataURL = /"base64DataUrl":"(.*?(?="))/gm.exec(string)[1];
console.log(dataURL);
Since the server is not returning valid JSON, you will have to parse it with indexOf and substring.
let y = stringFromServer;
let a = y.indexOf('data:');
let b = y.indexOf('\"}',a);
let dataUrl = y.substring(a,b);
Here is my try
let sample = {"href":"about:blank#&executeFunction<-finishedStroke&&arguments<-{\"base64DataUrl\":\"data:image/png;base64,iVBORw0KGgoAAErkJggg==\"}&"}
let splitter = sample.href.split("<-")
console.log(JSON.parse(splitter[2].substring(0, splitter[2].length - 1))["base64DataUrl"]
)

How to get this values form this data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
envelope:'{"to":["reply-to-501-4455#email.ashishbhangade.com"],"from":"Tway"}'
here my object data and i want find values only 501 and 4455 (every time this values are dynamic not static values) so how to get please give me suggestion.
This is way to dynamic values
const split = '{"to":["reply-to-501-4455#email.ashishbhangade.com"],"from":"Tway"}'.split('-')
const code1 = split[2]
const arrWithCode2 = split[3].split('#')
const code2 = arrWithCode2[0]
console.log(code1, code2)
If "reply-to-" and codes length don`t change
const msg = '{"to":["reply-to-501-4455#email.ashishbhangade.com"],"from":"Tway"}'
const code1 = msg.substring(17, 20)
const code2 = msg.substring(21, 25)
console.log(code1, code2)
Perhaps with string.includes?
const envelope = {"to":["reply-to-501-4455#email.ashishbhangade.com"],"from":"Tway"}
console.log(
envelope.to.some(pr => pr.includes("501")),
envelope.to.some(pr => pr.includes("4455"))
)

assign key correctly to value in javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to map a user defined key to the associated value and it is currently returning undefined for the final (var final). The goal is to collect a user provided input (key) and map it to the associated string from the key:value object and then load a URL using this value. The web page loads incorrectly in the end because the value is undefined. Here is the code:
document.getElementById("btn").onclick = function onLoad() {
var converts = {
'apples' : 'green',
'sky' : 'blue'
};
var myTextField = document.getElementById("myTextarea");
var itemName = myTextField.value;
var final = converts[itemName];
if (document.getElementById('rad1').checked) {
window.open("somewebsite" + final + "restofURL", "this is a new window");
}
}
The problem is that the textarea has a lot of white-spaces in it by default. If the user adds a space, that could cause problems so we need to trim the string:
convert[ itemName.trim() ];
This will remove all the bordering whitespace.

Categories

Resources