I having a problem with an array inside a JavaScript, and I don't how I can get the data from inside the array. This is the code:
array = [A(-11.8,166.88),A(-11.8,166.88),A(-11.8,166.88)]
function A(a,b) {
polyCoords.push(projTransform(a,b));
}
I want to get each lat and long separate in javascript format, but I don't know how I can proceed. Probably is more easier than I think, but currently I block with this.
Do you have any idea on how can I proceed?
The result that I want is to obtain this data.
array[0] = [-11.8,166.88]
array[1] = [-11.8,166.88]
array[2] = [-11.8,166.88]
Thanks in advance
var array = [A(-11.8,166.88),A(-11.8,166.88),A(-11.8,166.88)];
function A(a,b) {
//polyCoords.push(projTransform(a,b));
//you need to return a value, otherwise the result of the method
//is undefined
return [a, b];
}
console.log(array);
console.log(array[0][0], array[0][1]);
console.log(array[1][0], array[1][1]);
console.log(array[2][0], array[2][1]);
To me it looks like you want to define the array and then add it to polyCoords.
var array = [[-11.8,166.88],[-11.8,166.88],[-11.8,166.88]];
array.forEach(el => polyCoords.push(projTransform(el[0], el[1])));
Related
I have a data object, so I want to push an item into it, I understand that we can't push into an object so I want to convert the object into an array, push my item into it and then convert back to an object
{
Activity: 'digitalLoan',
digitalLoan_type: 'first_advance',
firstdvance_acc_number: '321e132e1'
}
How do I go about that?
I think you came to a conclusion without even trying it. Check code below:
var records = {
Activity: 'digitalLoan',
digitalLoan_type: 'first_advance',
firstdvance_acc_number: '321e132e1'
}
records.user_email = "adesanya"
console.log(records)
So here is something simple I did to achieve that.
Collect the data object
const records = context._activity.value;
console.log(records);
Convert the object to an array
var result = Object.keys(records).map(function (key) {
return { [key]: records[key] };
});
console.log(result);
Push some data
result.push({"user_email":"adesanya"})
Convert the result array back to object
var newRecs = Object.assign({}, ...result);
console.log(newRecs);
I have a json string
["https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"]
I need to get at the data only and I want to extract the string to get the following :
https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg
I have tried to use JSON.parse but this does not seem to work
Any help woul dbe appreciated
[] represents an array on JSON. {} represents an Object.
So in order to fetch the first element from you json string, you have to parse the string as a JSON element ;
var arr = JSON.parse('["https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"]');
OR when you HAVE a json array already;
var arr = ["https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"];
Then, go on and fetch the first value from your array which has index 0 as in all programming languages.
var url = arr[0];
It's seems to be a normal array not a JSON, but if you want you can treat it as JSON:
var image = JSON.parse('["https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"]')[0];
console.log(image); //https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg
Be aware of the difference between an array, and a JSON object.
I have given some examples of the differences and how to access them.
// This is an array containing 1 value.
myobj = ["https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"];
// it can be accessed by either the array name (since it only hase one value) or the array name and index of the value in cases where the array actually has more than one value.
var example1 = [myobj];
document.write("This is my single value array:<br>" + example1 + "<br><br>");
// This is probably best practice regardless of the number of items in the array.
var example2 = myobj[0];
document.write("Now im specificing the index, incase there are more that one urls in the array:<br>" + example1 + "<br><br>");
// This is a JSON object
myJsonObj = {"url":"https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"}
// You access the value of the URL like this:
var example3 = myJsonObj.url;
document.write("Here is the value from a JSON object:<br>" + example3 );
Hope this helps
Just use parse function:
var text = '["https://upload.wikimedia.org/wikipedia/commons/5/57/Cassini_Helene_N00086698_CL.jpg"]';
var obj = JSON.parse(text);
https://jsfiddle.net/esouc488/1/
Im using the following code to split array which is working,
I need to pass some value when array
for examle here is split the value to array
var myArr = val.split(/(\s+)/);
and if array in place 2 is empty I need to use the method like following
pass empty array in second arg
var val = process.run(myArr[0], [], options);
if the array place 2 is not empty I need to pass it like following
var val = process.run(myArr[0], [myArr[2]], options);
The second arg is array inside arry with the value of 2
there is nice way to do it instead of if on the method ?
I would create a function, as Dave Newton recommends. I could take the initial val and options as an argument and return the result of process.run:
function runProcess(val, options) {
var myArr = val.split(/(\s+)/);
var argArray = [];
if(myArr[2]) {
argArray.push(myArr[2]);
}
return process.run(myArr[0], argArray, options);
}
Since I don't know what the function exactly does, the name of the function and variables are pretty arbitrary. Feel free to change them to your needs.
If myArr[2] is a flat array and will always be flat, why not...
var val = process.run(myArr[0], [].concat(myArr[2]), options);
what I am really trying to do is add an array after an event only if that event occurred once like the press of the enter button. if the user keeps on entering the wrong answer it should only update the array once with the array that represents the question. if you can help me out with this question that deals with more of what i am trying to do would be great.
if you cant help me with that i am wondering how to insert an array after an event only if the array isn't in the array. can you show me some code where you search for an array inside of an array and if that array is inside do something or nothing?
I don't understand why in the below code i am not able to search for arrays
function onlyUnique(value, index ,self){
return self.indexOf(value) === index;
}
var a = ['a' , [1], [1],'a']
var unique = a.filter(onlyUnique)
console.log(unique)
You could use an object like an associative array. You would need some way to identify the questions to link to the answers.
// answers storage
var answers = {};
// set answer to question 1, this would be replaced by automation
var questionId = 'q1';
var data = 'some answer';
// insert if not present
if(answers[questionId] === undefined) {
answers[questionId] = data;
}
// manipulate existing answer
else {
// do something with answers[questionId]
}
There is not a simple operation to do a deep comparison of objects. However, you can try converting the objects to JSON and comparing - provided the JSON generated has the objects properties in the same order then it will generate identical strings which can be compared:
function onlyUnique(a){
return a.map(function(v){ return JSON.stringify(v); })
.sort()
.filter(function(v,i,a){ return i==0 || v !== a[i-1]; })
.map(function(v){return JSON.parse(v);});
};
var a = ['a' , [1], [1],'a']
var unique = onlyUnique(a);
console.log(unique)
I have data being pulled in from various sources, each returning some form of JSON or similar, although, differently formatted each time. I need to get them all into one array, but I can't figure out how to do it.
The first set is an array like this:
[
Object {id="70", type="ab", dateadded="12345678"},
Object {id="85", type="ab", dateadded="87654321"}, ... more items ...
]
The second set is being pulled in from Facebook, and is like this:
[
Object {id="12341234234", created_time="12345678"},
Object {id="567856785678", created_time="87654321"}, ... more items ...
]
So, I need to alter the second set so that it has 'type', and it has 'dateadded' instead of 'created_time', and then I need to get this all into one array so it can be sorted on 'dateadded'.
How can I do this?
Use the first array's push() method:
// for each item in second array
firstArray.push(convert(item));
function convert(obj) {
// Convert obj into format compatible with first array and return it
}
Hope this helps.
Assuming you have actual valid JSON instead of what you quoted above:
var jsonOld = '[{"id":"70","type":"ab","dateadded":"12345678"},{"id":"85","type":"ab","dateadded":"87654321"}]',
jsonNew = '[{"id":"12341234234","created_time":"12345678"},{"id":"567856785678","created_time":"87654321"}]';
Then first parse these values into actual Javascript arrays:
var mainArr = JSON.parse(jsonOld),
newArr = JSON.parse(jsonNew);
(If you already have actual Javascript arrays instead of JSON strings then skip the above step.)
Then just iterate over newArr and change the properties you need changed:
for (var i = 0, il = newArr.length; i < il; i++) {
newArr[i].type = 'ab';
newArr[i].dateadded = newArr[i].created_time;
delete newArr[i].created_time;
}
And concatenate newArr into mainArr:
mainArr = mainArr.concat(newArr);
And sort on dateadded:
mainArr.sort(function(a, b) { return a.dateadded - b.dateadded; });
This will result in:
[{"id":"70","type":"ab","dateadded":"12345678"},
{"id":"12341234234","type":"ab","dateadded":"12345678"},
{"id":"85","type":"ab","dateadded":"87654321"},
{"id":"567856785678","type":"ab","dateadded":"87654321"}]
See example