Answers for "...array js"

142

javascript array

//create an array like so:
var colors = ["red","blue","green"];

//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}
Posted by: Guest on July-01-2019
5

javascript array read object value in array

var events = [
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 1
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 2
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 3
  }
];
console.log(events[0].place);
Posted by: Guest on July-18-2020
5

array javascript

//Their are no lists in javascript, they are called arays,wich are basically the same thing

var fruits = ['Apple', 'Banana'];

console.log(fruits.length);
// 2
var first = fruits[0];
// Apple

var last = fruits[fruits.length - 1];
// Banana

fruits.forEach(function(item, index, array) {
  console.log(item, index);
});
// Apple 0
// Banana 1

var newLength = fruits.push('Orange');
// ["Apple", "Banana", "Orange"]

var last = fruits.pop(); // deletes orange
// ["Apple", "Banana"];

var first = fruits.shift(); // supprime Apple (au début)
// ["Banana"];

var newLength = fruits.unshift('Strawberry') // adds to the start
// ["Strawberry", "Banana"];

fruits.push('Mango');
// ["Strawberry", "Banana", "Mango"]

var pos = fruits.indexOf('Banana');
// 1

var removedItem = fruits.splice(pos, 1); // deletes on element at the position pos
//removes one element at the position pos
// ["Strawberry", "Mango"]




var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'];
console.log(vegetables);
// ["Cabbage", "Turnip", "Radish", "Carrot"]

var pos = 1, n = 2;

var removedItems = vegetables.splice(pos, n);
// n defines the number of element to delete starting from pos

console.log(vegetables);
// ["Cabbage", "Carrot"] 
console.log(removedItems);
// ["Turnip", "Radish"] (splice returns list of deleted objects)
Posted by: Guest on February-18-2021
4

javascript array

const arr = [1, 2, 3];
Posted by: Guest on October-04-2020
0

array

import java.util.Scanner;

public class BooleanPrimes {
    public static void main(String[] argh)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int size = scanner.nextInt();
        boolean[] boolarray = new boolean[size];
        generatorBoolArray(boolarray);
    }

    public static boolean[] generatorBoolArray(boolean[] boolArr)
    {
        for (int i = 2; i < boolArr.length; ++i)
        {
            boolArr[i] = true;
        }
        return chickIfIndexisPrime(boolArr, boolArr.length);
    }

    public static boolean[] chickIfIndexisPrime(boolean[] arrIsPrime, int input)
    {
        int start = 2;
        while (start <= input)
        {
            int i = 2;
            boolean isprime = true;
            while(i < start )
            {
                if(start%i == 0) {
                    isprime = false;
                }
                ++i;
            }
            if(isprime==true){
              for(int j=4; j<arrIsPrime.length ;++j)
              {
                  if(j%start ==0)
                  { j=j==start?++j:j;
                      arrIsPrime[j]=false;
                  }
              }
            }
            ++start;

        }
        printArray(arrIsPrime);
        return arrIsPrime;
    }



    public static void printArray(boolean[] arr){
        System.out.println("Prime numbers: ");
        int i=0,j = 0 ;
        while(i<arr.length){
            if(arr[i]==true){
                System.out.print(i+",");
            }
            ++i;
        }System.out.println();

        while(j<arr.length){
            System.out.print("{Index: "+j+" "+arr[j]+"} ");
            ++j;
        }

    }

}
Posted by: Guest on July-16-2021

Browse Popular Code Answers by Language