updating the dom with array in jquery [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Want to know how to update the dom with an array of list items, similar to:
var localPlaceholder = document.getElementById('localFiles');
for (i=0; i<entries.length; i++) {
var newList = document.createElement( "li" );
newList.nodeValue = entries[i].name;
localPlacholder.appendChild(newList);
}
but simplified in jquery using the append method.

Just map the array of entries into an array of jQuery objects and append them in one go:
var entries = [{name: "foo"},{name: "bar"}];
$('#localFiles').append($.map(entries, function(entry) {
return $('<li>', { text: entry.name });
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="localFiles" />
See also: jQuery.map()

Just enter the raw html as a string.
var localPlaceholder = $('#localFiles');
var numEntries = entries.length;
for (var i=0; i < numEntries; i++) {
localPlaceholder.append("<li>" + entries[i].name + "</li>");
}

Related

Add HTML elements before and after text coming from the database using js [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Like what you see in the picture, this problem that I tried to solve and I did not know how to search for it, so I drew it. I would like to put a text in the HTML language before every point and every element brings information from the database I mean by the data point is r[4] + r[5]
const column11 = document.createElement('p');
column11.setAttribute('class', 'card-text');
column11.textContent = 'Other' + r[4] + r[5]; // r[4] and r[5] should be wrapped with <code></code>
// Database array
const r = [,,,,'Text', 'Text']
const column11 = document.createElement('p');
column11.setAttribute('class', 'card-text');
column11.textContent = 'Other ';
const code1 = document.createElement('code');
code1.textContent = r[4];
const code2 = document.createElement('code');
code2.textContent = r[5];
column11.appendChild(code1);
column11.appendChild(code2);
document.body.appendChild(column11);

How to add an item in array to list of html? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
const toDoForm = document.querySelector(".todo__form"),
toDoInput = document.querySelector(".todo__input");
const toDoArray = [];
function addToDoList() {
toDoForm.addEventListener('submit', event => {
toDoArray.push(toDoInput.value);
event.preventDefault();
});
}
I made a code to put an input value to an array, and i want to put it in the list of html. How do i do that?
You can display the elements from your array dynamically by building a string and insert the string into the element.
const toDoArray = ["Max", "mortiz", "martin"];
function displayArrayInList(){
let output = `<ul>`
for(let i = 0; i < toDoArray.length; i++){
output += `<li>${toDoArray[i]}</li>`
}
output += `</ul>`
document.getElementById('displayer').innerHTML = output;
}
displayArrayInList();
<div id="displayer"></div>

Changing function name with array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Could someone say how to make an array of function names that will change in while loop?
Example of what I want:
var funcNames = ['function1()', 'function2()', 'function3()','function4()','function5()'];
var i = 0;
while( i < funcNames.length ) {
function funcNames[i] {
alert("hello");
}
i++;
}
In the example, the code doesn't work. It only shows how the code should work.
You can think about create new dynamic function name in javascript? like below:
var funcNames = ['function1()', 'function2()', 'function3()','function4()','function5()'];
var i = 0;
var result = [];
while( i < funcNames.length ) {
var funcName = funcNames[i];
var func = new Function("return function " + funcName + "{ alert('hello')}");
result.push(func);
i++;
}
console.log(result);
var firstFunc = result[0]();
console.log(firstFunc()); // invoke one func here

Receive a String in this format: "1-10" and create an array with the amount of numbers in the range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to Receive a String in this format: "1-10" and create an array with the amount of numbers in the range. Print the array to screen using a for loop.
I.E - "1-5" received so they array will be: {1,2,3,4,5}
create for workflow with vCenter orchestrator.
You can split the string into array and then iterate in a loop to get the iteration.
let str = "1-5";
str = str.split('-');
for(let i = parseInt(str[0]); i<=parseInt(str[1]); i++) {
console.log(i);
}
You may use some cool ES6:
Array.range = function(s){
const [start,end] = s.split("-");
return Array.from({length:start-end}).map((_,i)=>i+ +start);
};
Usable like this:
Array.range("1-10") //[1,2,3...]
var input = "1-10"; //SAMPE INPUT DATA.
var foo = input.split("-"); //PASRING INPUT DATA.
var answer = [];
for(var i = foo[0]; i<= foo[1]; i++){
answer.push(parseInt(i)); //MAKE AN ARRAY.
}
console.log(answer);

how to sort the object array to map with respect to fields in object array using java script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an object array like this
[
{bookName:"javascript",authorName:"john"},
{bookName:"java",authorName:"peter"},
{bookName:"j2ee",authorName:"john"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"}];
I tried the logics but i didnt get correctly.I want to sort out like this..
{
peter:{
[{bookName:"java",authorName:"peter"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"} ]
} ,
john :{
[{bookName:"j2ee",authorName:"john"},
{bookName:"javascript",authorName:"john"}]
}
}.
how can i do that.. Thanks in Advance ..
var obj={};
var objArr = given objectarray;
for(var i=0;i<objArr.length.i++ ){
objArr[objArr[i].authorName]=objArr[i];
}
and
var obj={};
var objArr = given objectarray;
for(var i=0;i<objArr.length.i++ ){
var arr=[];
objArr[objArr[i].authorName]=arr.push(objArr[i]);
}
You could do this:
var b = ... your object ...
var m = {};
b.map(function(v){(m[v.authorName]=m[v.authorName]||[]).push(v)});
Demonstration (open the console)
http://jsfiddle.net/azqRu/
var array = [
{bookName:"javascript",authorName:"john"},
{bookName:"java",authorName:"peter"},
{bookName:"j2ee",authorName:"john"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"}
];
var items = {};
for(var i = 0, ii = array.length; i < ii; i++) {
var cell = array[i];
items[cell.authorName] = items[cell.authorName] || [];
items[cell.authorName].push(cell);
}
alert(items["peter"]);

Categories

Resources