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());
```
Related
New to AngularJS so apologies if my terminology isn't correct. I am trying to call a value that is in my JSON body, however the difference is that it's also inside another body called student.
{
"id": 3,
"teacherName": "N/A",
"students": [
{
"student": "n/a"
}
],
"status": "ALIVE"
}
My JS code consists of the following:
$scope.table = function () {
SpringDataRestService.get(
{
collection: "school",
resource: $scope.teacherSelected.id
},
function (response) {
var students= [];
for (var j = 0; j < response.students.length; j++) {
var entitlement= {
student: response[j].student,
};
students.push(entitlement);
}
$log.info(JSON.stringify(entitlement))
$scope.tableOptions = new NgTableParams({}, {
dataset: students,
counts: []
});
}
);
};
There is an error in for loop. Instead of:
student: response[j].student
you need to use this:
student: response.students[j].student
Check this working demo: DEMO
I have an array and object of items, I want to check each item in that array if its path has that object name, I push it in that object array.
So far this is all good, now if no match found I want to create a new item based on that array item name and push it inside it!
All my attempts ending in duplicated value, I think I need a third object/array I just can't think it anymore
To explain better:
cList = {
"rList": {
"Significant": [
{
"Path": "Significant\\Significant Charts",
"Name": "Charts"
}
]
},
};
and
SSList = {
value: [
{
"Name": "Test long name",
"Path": "/someFolder/Test long name",
},
{
"Name": "Untitled",
"Path": "/Significant/Untitled",
}
]
};
My current code
for (var cFolder in this.cList.rList) {
this.SSList.forEach((ssFile)=> {
if(ssFile.Path.indexOf(cFolder) >= 0){
this.cList.rList[cFolder].push(ssFile);
}
});
}
The first item in SSList will not be pushed since it doesn't match, I want to create a array and push it to inside rList
var folderName = ssFile.Path.split("/");
this.cList.rList[folderName[1]].push(ssFile);
One way to do it is to flip your inner and outer loops
let found = false;
this.SSList.value.forEach((ssFile) => {
for (var cFolder in this.cList.rList) {
if(ssFile.Path.indexOf(cFolder) >= 0){
found = true;
break;
}
}
if (found) {
this.cList.rList[cFolder].push(ssFile);
} else {
folderName = ssFile.Path.split("/");
if (!(folderName[1] in this.cList.rList))
this.cList.rList[folderName[1]] = [];
this.cList.rList[folderName[1]].push(ssFile);
}
found = false;
});
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
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];
}
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
}