Using jquery.nestable.min.js, I get a the following output:
[{"id":32},{"id":29},{"id":30}]
This current output is coming from the following code:
const myList = JSON.stringify(list.nestable('serialize'));
I need it to simply be:
32, 29, 30
End result is that I would like to read a normal array in PHP. I'm looking to either convert myList to an array in Javascript and POST that array, or convert the current version of the object to an array inside PHP, whichever is most effective.
I've tried to use json_decode in PHP, but I get empty values. So I figured if I can just convert to a normal Array before sending it off to PHP, then it would be less of a hassle.
Thank you.
This may be duplicated, in which case, please point me to the best answer
Depending on whether you want an array (per your title), or literally 32, 29, 30 per your post:
console.log([{"id":32},{"id":29},{"id":30}].map(i => i.id))
console.log([{"id":32},{"id":29},{"id":30}].map(i => i.id).join(', '))
Which, with your example, is probably going to be:
const myList = JSON.stringify(list.nestable('serialize').map(i => i.id));
If you just want an array of numbers, convert that output array of objects to array of numbers
var list = [{"id":32}, {"id":29}, {"id":30}];
var required = list.map(item => item.id);
console.log(required);
I am using angular5.I have a questionOrderList which is a string which contains multidimensional array in string format. What I need to do is to parse the array in string format back to multidimensional array type. Currently I have used the JSON.parse method my code looks like.
console.log("qorder service : "+questionOrderList)
console.log("qorder service parsed:" +JSON.parse(questionOrderList))
The output I am getting is
qorder service : [[3290],[3287],[3289,3293],[3295]]
qorder service parsed: 3290,3287,3289,3293,3295
But in case of array with one row it is correct
qorder service : [[3290,3287,3289,3293,3295]]
qorder service parsed: 3290,3287,3289,3293,3295
What you are going is correct, see this:
var questionOrderList = "[[3290],[3287],[3289,3293],[3295]]";
var parsedList = JSON.parse(questionOrderList);
console.log(parsedList)
The actual issue is how JS interprets string concatination with arrays. It implicitly calls .join(',') on the array as demonstrated here:
var questionOrderList = "[[3290],[3287],[3289,3293],[3295]]";
var parsedList = JSON.parse(questionOrderList);
console.log(parsedList);
console.log("" + parsedList);
console.log(parsedList.join(','))
My code returns a JSON array, I think. The returned JSON array is stored in a javascript variable: result. If I
console.log(result);
in FF, I get the output
[{"id":"G24","value":"Zas, S"},{"id":"G75","value":"Wara, TS"},{"id":"G48","value":"Jala, S"}]
Validated on jsonLint to be correct json.
In my code, if I count the number of elements in the array like so:
var key, count = 0;
for(key in result)
{
count++;
}
console.log("result count is:" + count);
The output is 94 which is the length/count of characters in the array - [the sample output shown above is modified]
However, in the JSON tab in FF, it shows the result as being an array of objects:
0 Object { id="G24", value="Zas, S"}
1 Object { id="G75", value="Wara, TS"}
2 Object { id="G48", value="Jala, S"}
I have used alternative code pieces from 'stackoverflow' sources
for ( property in result )
{
if(result.hasOwnProperty(property))
{
count++;
}
}
And this has had the same outcome. How can I have iterate rightly over this array or array of objects or string or whatever else it is? And get the count please?. Thanks.
It sounds like you have an HTTP response returning a JSON document.
In the JSON tab, this is shown as JSON.
If your code, you are just taking the text of the response and operating on that.
You need to parse the JSON to create JavaScript objects.
Pass the string through JSON.parse and use json2.js to polyfill for older browsers.
You have to parse the JSON to create a JavaScript Array.
result = JSON.parse(result); // Now it's an array
console.log(result.length) // prints now what you want
I am been having trouble counting the number of objects in this array in server-side javascript.
Below is a JSON object which was parsed out using the array that I am trying to count.
NOTE: The object is in object form, not JSON string form.
JSON Object:
[{"dataSymbol":"21135103","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49652777778,"strikePrice":"33.00","last":"1.30","change":"0.20","changePct":"18.1818","lastRaw":1.3,"ask":"1.40","bid":"1.30","lastTime":40073.49652777778,"tick":0,"openInterest":"13.6K","volume":"80311","expDate":40194,"coName":"AJR Jan0 33.0 C"},
{"dataSymbol":"21339645","isHoliday":false,"isIPO":false,"lastTradeTime":40073.50479166866,"strikePrice":"6.00","last":"2.11","change":"0.01","changePct":"0.4762","lastRaw":2.11,"ask":"2.15","bid":"2.10","lastTime":40073.50479166866,"tick":0,"openInterest":"105.00","volume":"62313","expDate":40285,"coName":"EK Apr0 6.0 C"},
{"dataSymbol":"13511861","isHoliday":false,"isIPO":false,"lastTradeTime":40073.489583333336,"strikePrice":"113.00","last":"1.41","change":"-6.34","changePct":"-81.8065","lastRaw":1.41,"ask":"7.60","bid":"7.45","lastTime":40073.489583333336,"tick":0,"openInterest":"805.00","volume":"62975","expDate":40138,"coName":"SPY Nov8 113.0 P"},
{"dataSymbol":"20718334","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49375,"strikePrice":"40.00","last":"1.42","change":"-0.05","changePct":"-3.4014","lastRaw":1.42,"ask":"1.46","bid":"1.44","lastTime":40073.49375,"tick":0,"openInterest":"116.1K","volume":"60470","expDate":40194,"coName":"QQQQ Jan0 40.0 P"},
{"dataSymbol":"20348966","isHoliday":false,"isIPO":false,"lastTradeTime":40073.47708333333,"strikePrice":"41.00","last":"2.39","change":"-0.06","changePct":"-2.449","lastRaw":2.39,"ask":"2.45","bid":"2.42","lastTime":40073.47708333333,"tick":-1,"openInterest":"4.6K","volume":"60320","expDate":40257,"coName":"QQQQ Mar0 41.0 P"}]
I usually use myObject.length to count this type of array, but that is not working.
Response.Write(optionsQuotes.length);
The above code is returning a result of 21339646 as the count, when the actual count of the array is 5.
I would rather not have to loop through the array to count it, because I am looping through it later in order to draw a table, and I need to know the last iteration before the table draw begins.
Any ideas?
EDIT:
//here is where I am gettnig the array of objects...
var myObj = common.getMyObj("param1", "param2");
I serialized the object for the purpose of showing the contents of the array.
myObj.constructor is an Array.
This is on the server side also BTW.
ECMAScript doesn't handle the length of "assocative" arrays like PHP does - either use a real list that has a .length property, set the .length property manually in the JSON as you populate properties in the object, or do a for..in loop and make sure to use .hasOwnProperty and increment some counter.
Mhh... maybe is not a JSON object but an string and the length that is returning is the length of the string and not of the json array
With prototype you need to do something like
var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
data.length
but this obviously is depending of the framework that you are using.
The bug must be somewhere else as the following
<script>
var foo = eval('[{"dataSymbol":"21135103","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49652777778,"strikePrice":"33.00","last":"1.30","change":"0.20","changePct":"18.1818","lastRaw":1.3,"ask":"1.40","bid":"1.30","lastTime":40073.49652777778,"tick":0,"openInterest":"13.6K","volume":"80311","expDate":40194,"coName":"AJR Jan0 33.0 C"},{"dataSymbol":"21339645","isHoliday":false,"isIPO":false,"lastTradeTime":40073.50479166866,"strikePrice":"6.00","last":"2.11","change":"0.01","changePct":"0.4762","lastRaw":2.11,"ask":"2.15","bid":"2.10","lastTime":40073.50479166866,"tick":0,"openInterest":"105.00","volume":"62313","expDate":40285,"coName":"EK Apr0 6.0 C"},{"dataSymbol":"13511861","isHoliday":false,"isIPO":false,"lastTradeTime":40073.489583333336,"strikePrice":"113.00","last":"1.41","change":"-6.34","changePct":"-81.8065","lastRaw":1.41,"ask":"7.60","bid":"7.45","lastTime":40073.489583333336,"tick":0,"openInterest":"805.00","volume":"62975","expDate":40138,"coName":"SPY Nov8 113.0 P"},{"dataSymbol":"20718334","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49375,"strikePrice":"40.00","last":"1.42","change":"-0.05","changePct":"-3.4014","lastRaw":1.42,"ask":"1.46","bid":"1.44","lastTime":40073.49375,"tick":0,"openInterest":"116.1K","volume":"60470","expDate":40194,"coName":"QQQQ Jan0 40.0 P"},{"dataSymbol":"20348966","isHoliday":false,"isIPO":false,"lastTradeTime":40073.47708333333,"strikePrice":"41.00","last":"2.39","change":"-0.06","changePct":"-2.449","lastRaw":2.39,"ask":"2.45","bid":"2.42","lastTime":40073.47708333333,"tick":-1,"openInterest":"4.6K","volume":"60320","expDate":40257,"coName":"QQQQ Mar0 41.0 P"}]');
document.writeln(foo.length);
</script>
yields the correct value.
I think you need to eval the string. Could 21339645 be the number of characters?