moving one array into other array for handling ng-repeat - angularjs

var res =
{
"response": {
"data": {
"profilesearchsnippet": [
[
{
"profileInfo": {
"firstname": "Sundar",
"lastname": "v"
},
"roleInfo": {
"defaultphotoid": 94
}
}
],
[
{
"profileInfo": {
"firstname": "ghg",
"lastname": "vbhvh"
},
"roleInfo": {
"defaultphotoid": 171
}
}
]
]
}
}
$scope.profileData = [];
I have a response var res . I need to pass my defaultphotoid to another request and form URL for displaying the image. which I pushed in $scope.images and I need to display all list of images along with respective firstname and lastname. But I couldnt do it. somewhere I am lacking it.
I pushed firstname and lastname in array by creating object in $scope.profileData
$scope.searchData = res.response.data.profilesearchsnippet;
for (var i = 0; i < searchData.length; i++) {
$scope.profileData.push({
'fname':searchData[0].profileInfo.firstname,
'lname':searchData[0].profileInfo.lastname
});
}
The above $scope.profileData [] has exact values what I am pushing , But I could push the values of images from $scope.images into this.
$scope.profileData.push({'image': images[i]});
what happen in above case is the first array has fname and lname object and second array object has image.

When you push into an array, it is added as a separate object in the array. If you want the image to be added along with fname and lname, add it in the loop itself like :
$scope.profileData.image = images[i];
If you already has the $scope.images array, You can add the image too in the same for loop you are using to add lName and fName like this :
for (var i = 0; i < searchData.length; i++){
$scope.profileData.push({'fname':searchData[i].profileInfo.firstname,'lname':searchData[i].profileInfo.lastname, 'image':$scope.images[i]});
}

try like this
$scope.searchData = res.response.data.profilesearchsnippet;
for (var i = 0; i < searchData.length; i++){
$scope.profileData.push({'fname':searchData[0].profileInfo.firstname,'lname':searchData[0].profileInfo.lastname, 'image':''});
}
Then:-
for (var i = 0; i < $scope.images.length; i++){
$scope.profileData[i].image = $scope.images[i];
}

Related

Make a list from a multidimensional aray

I have an array of values, which I would like to put in an UL. To be specific some thing in an order like this :
< Li with the IDs > One more obj/ information from the arrray
Is there a way to import an image from an array, and how can I import the array to a list?
<div>
<ul id=cakeList>
</ul>
</div>
let cakes = [
{
"ID":"Strawberry" ,
"Name": "Vanilla Strawberry Cake",
"Eggs":false,
"Time":45,
"IMG":"https://cdn.shortpixel.ai/spai/q_glossy+ret_img+to_webp/https://omgchocolatedesserts.com/wp-content/uploads/2016/03/Strawberry-Shortcake-2.jpg" },
{
"ID": "choco1" ,
"Name": "Chocolate cake",
"Eggs":true,
"Time":120,
"IMG":"https://www.bbcgoodfood.com/sites/default/files/styles/recipe/public/recipe_images/recipe-image-legacy-id--1043451_11.jpg?itok=Z_w2WOYB", },
{
"ID": "cheesecake1" ,
"Name": "Strawberry cheesecake",
"Eggs":false,
"Time":30,
"IMG":"https://www.bbcgoodfood.com/sites/default/files/recipe-collections/collection-image/2013/05/recipe-image-legacy-id-1028453_10.jpg" },
];
function createList() {
var list = document.createElement('ul');
for (var i = 0; i < cakes.length; i++) {
for(var j=0, len2=cakes[i].length; j<len2; j++){
var item = document.createElement('li');
item.appendChild(document.createTextNode(cakes[i][j]));
list.appendChild(item);
}
return list;
}
}
document.getElementById('cakeList').appendChild(createList(cakes[i][1]));
https://jsfiddle.net/t0b3y0u/q9gck36o/37/
The first thing is that that array is not multidimensional but a simple array of objects. So what you need to do is first access each object itself and then the property you want to display:
function createList() {
var list = document.createElement('ul');
cakes.forEach(function(cake){
var item = document.createElement('li');
item.appendChild(document.createTextNode(cake.ID));
list.appendChild(item);
})
return list
}
document.getElementById('cakeList').appendChild(createList());
```

Empty array when looping json array in Nodejs

I want to get specific data from JSON array in adonisjs. But I have some problem to get that data. When I'm looping this JSON array the return just only empty array = []. This is my controller code:
const detail= await TrxHistory.query()
.where('id_trx', params.id_trx)
.fetch()
This return json array:
[
{
"id_trx_history": "1",
"id_trx": "3",
"trx_status": "shop_confirm",
"created_at": "2019-10-18 22:27:54"
},
{
"id_trx_history": "1",
"id_trx": "3",
"trx_status": "shop_process",
"created_at": "2019-10-18 22:29:48"
},
]
And i'm try to get data from row "trx_status", using looping like this:
let data = [];
for(var i = 0; i < detail.length; i++) {
data[i] = detail[i]["trx_status"];
}
console.log(data);
What's wrong with this code?
When you fetch adonis lucid request you need to use .rows. Like:
... // Your query
let fooData = [];
detail.rows.forEach(de => {
fooData.push(el.trx_status)
})
How to use?
detail.rows[1]
detail.rows.length
If detail has a structure that you've shown, then it should work perfectly:
const detail = [
{
"id_trx_history": "1",
"id_trx": "3",
"trx_status": "shop_confirm",
"created_at": "2019-10-18 22:27:54"
},
{
"id_trx_history": "1",
"id_trx": "3",
"trx_status": "shop_process",
"created_at": "2019-10-18 22:29:48"
},
];
let data = [];
for (let i = 0; i < detail.length; i++) {
data[i] = detail[i]["trx_status"];
}
console.log(data);
// another way:
let fooData = [];
detail.forEach(el => {
el["trx_status"] ? fooData.push(el["trx_status"]) : null;
});
console.log(fooData);

How dynamically transform my "Object" to List in ng-model at view

I'm trying to transform my object to list dynamically, so I'm building at view instead of declaring at controller.
I don't want to declare like this: custom_fields.title_field.type_text_field = [] because the title_field is built dynamic, it could be any kind of text like full_name
My json as is:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":{
"name":"John",
"first_name":"Wick"
},
"type_boolean_field":{
"is_badass": true,
"is_good_movie": true
},
"type_select_field": {
"this_select": 1,
"i_got_this": "nope i didnt got this"
}
},
And to be:
"custom_fields":{
"title_dynamic_generate_field":{
"type_text_field":[{
"name":"John",
"first_name":"Wick"
}],
"type_boolean_field":[{
"is_badass": true,
"is_good_movie": true
}],
"type_select_field": [{
"this_select": 1,
"i_got_this": "nope i didnt got this"
}]
},
the object I'm trying to transform into array is type_text_field which can be dynamic too, like type_date_field or type_select_field and so on.
My ng-model is like this:
ng-model="objectApp.application.applicant.custom_fields[layout.dynamic_title][input.type][input.variable]"
the [input.type] is that I'm trying to transform into array, how can I achieve this? I tried to use $index, but got strange results.
We can do it by 2 solutions:
There is a question about your task:
? how you want handle if we have more than one type_text_field in title_dynamic_generate_field? because you want to convert it to "type_text_field":[{},...]
however my answers about the question are:
If we know what's the dynamic params which we want to send theme as json, i mean if we know what is the key of title_dynamic_generate_field or type_text_field, we do as this sample:
var data = {
"custom_fields": {
dynamicParamIs1: 'title_dynamic_generate_field',
dynamicParamIs2: 'type_text_field',
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var paramHelper1 = json.custom_fields[json.custom_fields.dynamicParamIs1];
var paramHelper2 = json.custom_fields.dynamicParamIs2;
var solutionA = function (object, as) {
var array = [];
for (var key in object) {
var newObject = object[key];
array.push(newObject);
}
object[as] = array;
}
solutionA(paramHelper1, paramHelper2);
We changed a model of our json which can help us to detect (find) the keys
If we don't know what is the dynamic params are, we do as this:
var data = {
"custom_fields": {
"title_dynamic_generate_field": {
"type_text_field": {
"name": "John",
"first_name": "Wick"
}
}
}
}
var solutionB = function (json) {
var array = [];
for (var key in json) {
var j1 = json[key];
for (var key2 in j1) {
var j2 = j1[key2];
for (var key3 in j2) {
var fullObject = j2[key3];
array.push(fullObject);
j2[key3] = array;
}
}
}
}
solutionB(data);
This sample is manual which we use nested for to detect the keys name

How to push some data from Json into new array in AngularJS?

Let's say I have this Json ..
{
"name": "Mark",
"gender": "male",
"account1": {
"accountNo": 1201,
"balance": 300
},
"account2": {
"accountNo": 1354,
"balance": 5000
}
}
What I expect is like ..
$scope.myArray = [
{
"accountNo": 1201,
"balance": 300
},
{
"accountNo": 1354,
"balance": 5000
}
];
In AngularJS, how can I pick some part of Json data and push it into an array iteratively( I mean, when I have account1, account2 account3 or more, it can still add them into the array).
You could normally just assign the array over, but in this scenario that is not an option because your array is psuedo.
Ideally you would like to be able to do what this answer (related question) does: How to return and array inside a JSON object in Angular.js which is simply
$scope.myArray = json.accounts;
However, as noted, you do not have an accounts array so you need to make one.
var accounts = [];
for(var key in json){
if( !json.hasOwnProperty(key) // skip prototype extensions
|| !json[key].hasOwnProperty("accountNo") //skip non account objects
) continue;
accounts.push(json[key]);
}
And now you may use this array
$scope.myArray = accounts;
You can access Json data like an array. Like var foo = { ... , 'bar' = 'value', ... } you could get foo value by doing this for['bar']. So, by knowing this, you simply do something like
var arr = [];
for (var i = 0; i < 10; i++) {
arr.push(foo['account'+i]);
}
Although, this has nothing to do with angularjs.

getColumnArray equivalent for parsed JSON object arrays in Smartface.io Framework?

I got a Json file like;
{
"Cities": [
{
"Name": "London",
"Country": "UK"
},
{
"Name": "Rome",
"Country": "ITA"
},
{
"Name": "Antalya",
"Country": "TR"
}
]
}
How can I get City Names as an array like ["London","Rome","Antalya"] without doing;
var tempJSON = JSON.parse(jsonCities);
var arrayCityNames = [];
for (var i = 0; i < tempJSON.Table.length; i++){
arrayCityNames[i] = tempJSON.Table[i].Name;
}
if tempJSON was a Dataset we could use getColumnArray
arrayCityNames = Data.Dataset.getColumnArray("Name");
Is there any built in method to do this for parsed JSON's?
Please keep in mind that the question is related to Smartface.io Framework, not jquery itself
Try this:
var tempJSON = JSON.parse(jsonCities);// here you load your JSON
var arrayCityNames = []; // your output array
var cityArray = tempJSON['Cities']; // enter Cities array
for (var i = 0; i <cityArray.length; i++){ // iterate over your list
arrayCityNames.push(cityArray[i]['Name']); // add to list name of your city list
}

Resources