javascript from string to array. split my regex [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need give from text [{"value":"opa"},{"value":"opa2"},{"value":"opa3"}]
only opa...
and set it to array like var array = date.split(regex);
that array[0]="opa" and array[1]="opa2".
Please help me.

var a='[{"value":"opa"},{"value":"opa2"},{"value":"opa3"}]';
var b=JSON.parse(a);
c = [];
b.forEach(function(entry) {
c.push(entry.value);
});
alert(c[0]);

You have array of objets, the task is filter and turn this objets into strings.
var array = [{"value":"opa"},{"value":"opa2"},{"value":"o1pa3"}];
array = array.map(function(item){
if(/opa/.test(item.value)){
return item.value;
}
else return null;
}).filter(function(item){ return item; });
console.log(array)
If you want to parse string and convert to JSON object just use
var array = JSON.parse(STRING);
You can play with this demo

Related

Filter through an array of objects [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 3 years ago.
Improve this question
Having an array of objects with this structure
My question is how should a filter it to return, for example, only the objects with meterName = "Bay-3". That hexadecimal header at each object tangles me.
you can get object key using Object.keys() and then process the data
const result = data.filter(datum => {
const key = Object.keys(datum)[0];
return datum[key]['meterName'] === 'Bay-3';
})

Decode HTML Entities in Javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a record in the database like: <script src="here is link"></script>.
It's regexp for convert slash to special HTML char: string.replace(/\<\/script\>/g, '</script>');
Then I output it to the page.
It's result after output:
<script src="http://code.jquery.com/jquery-latest.min.js">
</script>
</script>
Why outputs HTML char? I need convert it again with regexp?
Use these JavaScript functions:
function decodeHTMLEntities(str) {
return str.replace(/&#(\d+);/g, function(match, dec) {
return String.fromCharCode(dec);
});
};
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''));
}
return buf.join('');
};
alert(decodeHTMLEntities('</script>'))
DEMO http://jsfiddle.net/tuga/6pXmn/3/
SRC https://gist.github.com/CatTail/4174511

JSON array into JavaScript array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i am trying to convert json array
{"id":"1","name":"abc"},
{"id":"2","name":"pqr"},
{"id":"3","name":"xyz"};
into this kind of js array
var locations = [
[1, 'abc'],
[2, 'pqr'],
[3, 'xyz']
];
I'm assuming you have the elements objects, (and not as Json as you state):
var data = [{"id":"1","name":"abc"},
{"id":"2","name":"pqr"},
{"id":"3","name":"xyz"}];
You can the convert it to a two-dimensional array like this
var output = new Array();
for (var i = 0; i < data.length; i++) {
output[i] = new Array(data[i].id, data[i].name);
}
You can do this like this.
var jsondata=[{"id":"1","name":"abc"},
{"id":"2","name":"pqr"},
{"id":"3","name":"xyz"}];
var arrayObj=$.parseJSON(jsondata);

Dynamically comparing elements of the array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Can someone please tell me how to compare elements in array with every other element. I mean in array arr = [a,b,c,d];, I would like to compare a with b,c,d , b with a,c,d, etc. And to do that dynamically, no mather of the size of the array.
Try this:
var a=["a","b","c","d"];
for(var i=0;i<a.length;i++){
for(var j=0;j<a.length;j++){
if(i!=j && a[j]===a[i]){
//match, do whatever you want
}
}
}

How to split each word from a string and store it in different variables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So here's the case, I have a variable with a string
var text = "This is a string";
Is it possible to take every word, except for the spaces, " ", of this string and put it into a seperate variable? Like for example:
var 1 = "This";
var 2 = "is";
var 3 = "a";
var 4 = "string";
Thanks in advance.
var text = "This is a string";
var splitted = text.split(" ");
console.log(splitted);
Output
[ 'This', 'is', 'a', 'string' ]
You can access the individual elements like this
console.log(splitted[1]); // will print is

Categories