Related
I have an array that looks like this
array_names = [Mathew,Kelp ,Liman Kolf, Hebert,Ansh];
Now in the array above, Mathew, Kelp is one name , Liman Kolf is another name and Herbet,Ansh is another name making it 3 names in the array.
Now i want to split this array on new lines in table like below
Mathew,Kelp
Liman Kolf
Herbet,Ansh
But with my code as shown below, the table is represented like this
Mathew
Kelp
Liman Kolf
Herbet
Ansh
JS
//how i save to localstorage
$("#myTableID").on("click", "#add-contact", function() {
var values = [];
value = jQuery(this).closest('tr').find('#user-id').text();
values.push(value);
localStorage.setItem('contact_list', values);
}
var array_names = localStorage.getItem('contact_list').split(',');
if(array_names.length)
{
$.each(array_names, function(index, value){
$('#myTableID2').append('<tr><td id="contact">'+value+'</td></tr>');
});
}
Controller
$contacts = Contact::where('firstname','LIKE','%'.$q.'%')->orWhere('lastname','LIKE','%'.$q.'%')->get();
You should split them by a RegExp
const string = 'Mathew,Kelp ,Liman Kolf, Hebert,Ansh';
const array = string.split(/(?: ,)|(?:, )/)
// ["Mathew,Kelp", "Liman Kolf, Hebert,Ansh"]
As suggested by #Khauri, use JSON.stringify() and JSON.parse() to read/write from/to localStorage:
// save values to localStorage
localStorage.setItem('contact_list', JSON.stringify(values));
// retrive values from localStorage
var array_names = JSON.stringify(localStorage.getItem('contact_list'));
// do things with the values
if (array_names.length) {
$.each(array_names, function (index, value) {
$('#myTableID2').append('<tr><td id="contact">' + value + '</td></tr>');
});
}
var data = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';
Here I have posted my JSON data. I want to get all the defaultValue in JSON/Array format. My output should be like-
defaultValues:['Size30','Black','Low']
How to manage that in the foreach loop?
my code :
var otherSelectedOption;
angular.forEach(data, function(optionValue, optionKey) {
if (optionValue.defaultValue.text) {
otherSelectedOption = (optionValue.defaultValue.text);
}
selectedOption = {defaultValues: otherSelectedOption};
console.log(selectedOption);
});
Your JSON is not valid, since objects are not separated by comma ,
Suppose this is the JSON
var obj = '[{"type":"product","id":1,"label":"Size","placeholder":"Select Size","description":"","defaultValue"
:{"text":"Size30","price":"20"},"choices":[{"text":"Size30","price":"20","isSelected":"true"},{"text"
:"Size32","price":"22","isSelected":false},{"text":"Size34","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":2,"label":"Color","placeholder":"Select Color","description":"","defaultValue"
:{"text":"Black","price":"10"},"choices":[{"text":"Black","price":"10","isSelected":"true"},{"text"
:"Green","price":"22","isSelected":false},{"text":"Red","price":"28","isSelected":false}],"conditionalLogic"
:""},{"type":"product","id":3,"label":"Rise","placeholder":"Select Rise","description":"","defaultValue"
:{"text":"Low","price":"8"},"choices":[{"text":"High","price":"12","isSelected":"true"},{"text"
:"Low","price":"8","isSelected":false}],"conditionalLogic"
:""}]';
try
var arr = JSON.parse(obj).map( function(item){
return item.defaultValue;
});
I wrote a small api to pull nutrition info from public diaries on MyFitnessPal. I'm pulling this information into a Google Sheet using the ImportJSON.gs found here: https://gist.github.com/paulgambill/cacd19da95a1421d3164
What I get is a two row result:
Calories Protein Fat Carbs
2458 196 82 234
My returned json looks like this:
{"Calories":"1738","Protein":"140","Fat":"78","Carbs":"119"}
I want just the numbers and not the property names. I don't want to alter the json to just return a string array, but since this is for personal usage, I will if necessary! Any ideas?
var o = {"Calories":"1738","Protein":"140","Fat":"78","Carbs":"119"}
var values = values(o); //gives you an array of values
Obviously you know how to retrieve the JSON. Just loop through the object:
function convert() {
var myJson = {"Calories":"1738","Protein":"140","Fat":"78","Carbs":"119"};
var thisKey;
var thisValue;
var newArray = [];
for (var key in myJson) {
thisKey = key;
thisValue = myJson[thisKey];
newArray.push(thisValue);
};
return newArray;
};
This creates an array of just the values.
returns: [1738, 140, 78, 119]
Simply push each value into a new array:
var values = [];
for (var i in myJson){
values.push(myJson[i])
}
I would like to push the elements into an array in a desired format
My code is :
var arr = [];
for(var i = 0; i < $scope.reportRangeList.length; i++)
{
arr.push($scope.reportRangeList[i].businessName , $scope.reportRangeList[i].total1);
alert(JSON.stringify(arr));
}
where $scope.reportRangeList is a JSON dynamic object
for the above the code i got the output like :
[
"abc",1820,
"pqrs",2349.67
]
Now i want to display the output like :
[
"abc",1820
],
[ "pqrs",2349.67
]
Please help me to display output in a desired format in java script or angular or j query .
You can push an array like
arr.push([$scope.reportRangeList[i].businessName , $scope.reportRangeList[i].total1]);
For that format push an array every time like bellow
arr.push([$scope.reportRangeList[i].businessName , $scope.reportRangeList[i].total1]);
Your desired format
var first = $scope.reportRangeList[i].businessName,
second = $scope.reportRangeList[i].total1,
text;
arr.push([first, second]);
text = JSON.stringify(arr);
text = text.substring(1, text.length - 1);
alert(text);
JSFiddle
Hi i have a Json like below format .I want to replace the value of text:"Sample Text" Sample text with a value from My array in js file.
JSON Format
prx.xdata = {"cc":6,
"symbols":[
{
"id":1,
"title":"news container",
"states":[
{"title":"Default State",
"background":"none",
"data":"[{\"name\":\"text\",\"type\":\"text\",\"lib\":\"common\",\"caption\":\"newsitem1\",
\"text\":\"Sample text\",\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",
\"textColor\":\"000000\",\"backgroundColor\":\"none\",\"width\":292,\"height\":257,\"textProperties\":[],
\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":8,
\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",\"visible\":true,\"id\":\"box-2\",
\"groups\":[],\"zindex\":2001,\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\"},
{\"name\":\"text\",
\"type\":\"text\",\"lib\":\"common\",\"caption\":\"news item2\",\"text\":\"Sample text\",\"textFont\":\"sans-serif,Helvetica Neue,
Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",\"backgroundColor\":\"none\",\"width\":301,\"height\":257,\"textProperties\":[],
\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":300,\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",
\"visible\":true,\"id\":\"box-3\",\"groups\":[],\"zindex\":2002,\"wtype\":\"fixed\",\"htype\":\"fixed\",\"opacity\":\"1\"},
{\"name\":\"text\",\"type\":\"text\",\"lib\":\"common\",\"caption\":\"news item3\",\"text\":\"Sample text\",
\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",
\"backgroundColor\":\"none\",\"width\":292,\"height\":257,\"textProperties\":[],\"textAlign\":\"left\",
\"enableShadow\":false,\"autoResize\":false,\"left\":601,\"top\":21,\"vpos\":\"top\",\"hpos\":\"left\",
\"visible\":true,\"id\":\"box-4\",\"groups\":[],\"zindex\":2003,\"wtype\":\"fixed\",
\"htype\":\"fixed\",\"opacity\":\"1\"},
{\"name\":\"text\",\"type\":\"text\",
\"lib\":\"common\",\"caption\":\"news item4\",\"text\":\"Sample text\",
\"textFont\":\"sans-serif,Helvetica Neue,Arial\",\"textSize\":\"16\",\"textColor\":\"000000\",
\"backgroundColor\":\"none\",\"width\":298,\"height\":257,\"textProperties\":[],
\"textAlign\":\"left\",\"enableShadow\":false,\"autoResize\":false,\"left\":901,\"top\":21,
\"vpos\":\"top\",\"hpos\":\"left\",\"visible\":true,\"id\":\"box-5\",\"groups\":[],\"zindex\":2004,\"wtype\":\"fixed\",
\"htype\":\"fixed\",\"opacity\":\"1\"}]",
"history":[],"dimensions":["1200","300"],"id":"a8d0d79e-1921-4f7e-a229-75e5b1602881"
}]
,"sort":0,
"customguides":{"horizontal":[],"vertical":[]},
"grid":{"col_number":3,"col_width":80,"gutter_width":20,"margins":10}
}]
};
prx.xdata_str = JSON.stringify(prx.xdata);
I want to replace the value of "Sample text " in this path
symbols[0].states[0].data[0].text with value from News array.
Problem is here
symbols[0].states[0].data this data is another JSON ,so am not able to parse that data.
Please check my Js code
function yourJsFunction(arr){
var b=arr.toString().split(',');
var News =new Array();
News=b;
var jsondata = JSON.parse(prx.xdata);
//alert(jsondata.symbols[0].states[0].data);
var data=new Array();
data=jsondata.symbols[0].states[0].data;
// alert(data);
var newData=JSON.parse(data);
newData[0].text=News[0];
newData[1].text=News[1];
newData[2].text=News[2];
newData[3].text=News[3];
alert(newData[0].text);
//This alert showing my updated json.But when i click ok in alert the webview showing previous Json
jsondata.symbols[0].states[0].data= JSON.stringify(newData);
prx.xdata = JSON.stringify(jsondata);
for(var i=0;i<arr.length;i++){
// document.write(arr[i]);
}
}
var x = "{a: 'a', b: 'b'}" this is an json string
var o = eval('"' + x + '"') o is json object, parsed.