How to convert multiple array item objects to single set of array - html

i have array like this
$scope.array1=[[Name:'Madhu'],[Name:'Vijay'],[Name:'Srinu']]
how to convert this array to this format,
i want to pass this array in columns option(in dx datagrid columns option)
so i want array format like this
$scope.array2=['Maadhu','vijay','srinu']

you could use the map extension on arrays in javascript:
$scope.array2 = $scope.array1.map(function(item) {
return item.Name;
});

you can use the map function:
$scope.array2 = $scope.array1.map(x = x.map(y => y.Name));

Related

laravel-translatable: converting existing text column to translatable

i'm trying to convert and existing text column to translatable. I find that when i add the column name to the the protected translatable array i am no longer able to access it as i did before ($model->key)
I assume that this is because its looked for a translation but can't find one. Is there a way for me to return to contents of the column? I want to retrieve the text and and replace it with a json
when I log $this i can see my object and the correct key: value pairs. Any attempt to access it or convert it to array causes the value to disappear completely
$array = json_decode(json_encode($this), true);
$object = json_decode(json_encode($this), false);
error_log('$this '.print_r($this,true)); // includes the key 'myKey' with correct value
error_log('$array '.print_r($array['mykey'],true)); // empty
error_log('$object '.print_r($object->mykey,true)); // empty
You can use this method if you want to get all translated values of a particular column as an array.
public function update(ModelName $modelItem)
{
return $modelItem->getTranslations('column_name');
}
//result
[
'en' => 'test',
'tr' => 'deneme',
]
Resource:
https://github.com/spatie/laravel-translatable#getting-all-translations-in-one-go
if you want to get the content that still not store as json translation, you can use this eloquent method.
$model->getRawOriginal('your translation's column name');
it will get your column value.

How can I get access to multiple values of nested JSON object?

I try to access to my data json file:
[{"id":1,"name":"Maria","project":[{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}]}
This is my approach:
data[0].name;
But like this I get only the result:
Animals
But I would need the result:
Animals, Cats
You are accessing only the name property of 0th index of project array.
To access all object at a time you need to loop over the array.
You can use Array.map for this.
var data = [{"id":1,"name":"Maria","project":[{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}]}]
var out = data[0].project.map(project => project.name).toString()
console.log(out)
If that's your actual data object, then data[0].name would give you "Maria". If I'm reading this right, though, you want to get all the names from the project array. You can use Array.map to do it fairly easily. Note the use of an ES6 arrow function to quickly and easily take in the object and return its name.
var bigObject = [{"id":1,"name":"Maria","project":[{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}]}];
var smallObject = [{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}];
console.log("Getting the names from the full array/data structure: "+bigObject[0].project.map(obj => obj.name))
console.log("Getting the names from just the project array: "+smallObject.map(obj => obj.name))
EDIT: As per your comment on the other answer, you said you needed to use the solution in this function:
"render": function (data, type, row) {if(Array.isArray(data)){return data.name;}}
To achieve this, it looks like you should use my bottom solution of the first snippet like so:
var data = [{"id":5,"name":"Animals"},{"id":6,"name":"Cats"}];
function render(data, type, row){
if(Array.isArray(data)){
return data.map(obj => obj.name);
}
};
console.log("Render returns \""+render(data)+"\" as an array.");

JSON - look up values in array

With the following json
{
"Count":0,
"Message":{
"AppId":0
},
"Data":"[{\"application_name\": \"Grand Central\",\"feature_name\": \"1 Click Fix\",\"access_type_id\": 2,\"member_name\": \"GC_Remote_Support_Security\"},{\"application_name\": \"Grand Central\",\"feature_name\": \"Account Details\",\"access_type_id\": 2,\"member_name\": \"GC_Remote_Support_Security\"},{\"application_name\": \"Grand Central\",\"feature_name\": \"Account Summary\",\"access_type_id\": 2,\"member_name\": \"GC_Remote_Support_Security\"}]"
}
how do I go through the Data array, in the most succinct coding manner possible, to see if any feature_name matches a given string?
Since your JSON contains nested, quoted JSON, you will need nested deserializations using LINQ to JSON to parse your Data array. Having done so, you can use use SelectTokens to query with a JSONPath query to find nested properties named feature_name, then check their value:
var testString = "Account Summary";
var found = JToken.Parse(JObject.Parse(jsonString)["Data"].ToString()).SelectTokens("..feature_name").Any(t => (string)t == testString);
Debug.Assert(found == true); // No assert.
Update
If you want the all JObject with a "feature_name" property matching a given value, you can do:
var foundItems = JToken.Parse(JObject.Parse(jsonString)["Data"].ToString())
.SelectTokens("..feature_name")
.Where(t => (string)t == testString)
.Select(t => t.Ancestors().OfType<JObject>().First()) // Get the immediate parent JObject of the matching value
.ToList();

Compare two arrays in Ruby and create a third array of elements

I have two arrays, array1 and array2. I need to compare both of these arrays and I want to create a third array, array3, whereby it shows the elements that are in array2, that are not in array1.
This is what I have so far:
my_buckets = Model.select("DISTINCT bucket").where(["my_id = ?", params[:user]])
all_buckets = Model.select("DISTINCT bucket").collect { |x| x.bucket }.uniq.compact
buckets_not_in_my_buckets = Model.select("DISTINCT bucket").where(["bucket NOT IN (?)", my_buckets]).collect { |x| x.bucket }.uniq.compact
For some reason, the buckets_not_in_my_buckets is always returning an empty array ([]). Is there a better way to approach this? Any help would be appreciated.
buckets_not_in_my_buckets = all_buckets - my_buckets
I'm assuming that you have the eql? operator on your buckets object working how you'd like.
Please see the Array docs for more detail.

SortOn an m-array of objects

Alright, so I have an m-array (Or an array of arrays in actionscript as it doesn't really have m-arrays) and each array in the superarray has a number of objects created at different times in it. I want to sort the superarray by in descending order of the value of the "time" paramater of the object at index 0 in each subarray.
I've tried
superarray.sortOn([0].time, Array.DESCENDING);
and
superarray.sortOn("0.time", Array.DESCENDING);
but this doesn't seem to work. Any suggestions? Will I just have to write my own sort function to do this? If so what's the best way to go about it?
Try using the Array.sort function passing a compare function. Something like this:
var superarray:Array = [
[{time:900}, {time:715}, {time:655}],
[{time:450}, {time:333}, {time:100}],
[{time:999}, {time:75}, {time:30}]
];
var sorted:Array = superarray.sort( function(A:Array,B:Array):int {
return ObjectUtil.numericCompare(A[0].time, B[0].time);
});