Javascript Array non empty array with 0 length [duplicate] - javascript

This question already has answers here:
console.log() shows the changed value of a variable before the value actually changes
(7 answers)
Closed 6 years ago.
I'm using knockout js to create a list with a search function.
Result of the console.log:
As you can see the array supposedly has 5 objects in it but then the proto has 0. I don't understand what might be wrong.
var iniList = [
new Spot("Park"),
new Spot("High School"),
new Spot("Soccer Stadium"),
new Spot("Railway Station"),
new Spot("Hospital")
];
var viewModel = {
spots: ko.observableArray(iniList),
filter: ko.observable(''),
search: function(value) {
console.log(iniList);
viewModel.spots.removeAll();
for (x = 0; x < iniList.length; x++) {
console.log("iniList[x]");
if (iniList[x].name.toLowerCase().indexOf(value.toLowerCase()) >= 0) {
viewModel.spots.push(iniList[x]);
}
}
}
};

You're running "viewModel.spots.removeAll();" This removes all elements ofthe observable array.

Related

Fastest and Most Efficient way to check for duplicates in a javascript array [duplicate]

This question already has answers here:
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
(97 answers)
In Javascript, how do I check if an array has duplicate values?
(9 answers)
Checking for duplicate strings in JavaScript array
(13 answers)
Closed 5 months ago.
I am writing a javascript function that takes a nested array and returns the numbers that occurs more than once in that array.
I believe my function is accurate (meaning that it passes their "Correctness test" ) but i am after efficiency, how efficient is this code?
For example - Lets call the name of the function deepSort(nestedArray) where nestedArray is the nested array parameter
function deepSort(nestedArray) {
const flatArr = nestedArray.flat().sort();
let results = []
for (let i = 0; i < flatArr.length - 1; i++) {
if (flatArr[i + 1] == flatArr[i]) {
results.push(flatArr[i]);
}
}
return (results.filter((item, index) => results.indexOf(item) === index)).join()
}
const a = deepSort([[1,3,4,5], [4,7,9,1,3], [2,3,5], [1,2,3,4]]) // Returns 1,2,3,4,5
console.log(a);
const b = deepSort([[1,2,3], [4,5], [6,7,8], [2,9,0]]) // Returns 2
console.log(b);
const c = deepSort([[2,7,9], [4,3], [9,6,5], [1,4,3]]) // Returns 3,4,9
console.log(c);
Can this code be optimized any more for speed and efficiency when handling extremely large values of data?

Function that creates an array [duplicate]

This question already has answers here:
How to create an array containing 1...N
(77 answers)
Fastest way to fill an array with multiple value in JS. Can I pass a some pattern or function to method fill insted of a single value in JS? [duplicate]
(1 answer)
Closed 4 years ago.
I want to create a function that takes input from user and returns an array with all the numbers from 1 to the passed number as an argument. Example: createArray(10) should return [1,2,3,4,5,6,7,8,9,10]. I came up with this solution:
function createArray(input) {
var value = 0;
var array = [];
for (i=0;i<input;i++) {
value++;
array.push(value)
console.log(array)
}
}
createArray(12);
What is the correct and better way of doing it?
I would prefer to use Array.from:
const createArray = length => Array.from(
{ length },
// Mapper function: i is the current index in the length being iterated over:
(_, i) => i + 1
)
console.log(JSON.stringify(createArray(10)));
console.log(JSON.stringify(createArray(5)));
There is no need for the extra variable just do this:
function createArray(input) {
var array = [];
for (i = 0; i <= input; i++) {
array.push(i);
}
return array;
}

Javascript: Setting all array values to the same [duplicate]

This question already has answers here:
Initializing an Array with a Single Value
(12 answers)
Closed 8 years ago.
I want to make a new array (of X elements) with all the elements set to 0.
X is set before.
Anyone could make the most compact and easy code for that? Thank you for your
help.
Just create a function:
function makeArray(size, defaultValue) {
var arr = [];
for (var i = 0; i < size; i++) arr.push(defaultValue);
return arr;
}
var myArr = makeArray(10, 0);

Need help understanding how arrays work in javascript for objects [duplicate]

This question already has answers here:
Does JavaScript pass by reference? [duplicate]
(13 answers)
Closed 8 years ago.
I am having trouble understanding how arrays work in javascript.
Lets say I have an object, car
car = {
moving: false,
wheels: 4
};
Lets say I want to create an array now, of 5 of these cars. How would you do that? I would want to use a for loop to create them, but I have read many pages on js arrays such as http://www.w3schools.com/js/js_arrays.asp and I am still stumped.
I tried doing
carArray = [];
for(int i=0; i < 5; i++)
{
carArray.push(car);
}
However, when the program runs, there is only one car, not 5, and it is at the last entry of carArray.
The word int should be removed. Then the code should work.
carArray = [];
for (i = 0; i < 5; i++) {
car = {
moving: false,
wheels: 4
};
carArray.push(car);
}

Sort Javascript Object [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting JavaScript Object by property value
Sort JavaScript object by key
I've written a function called frequency() that takes an Array, counts the number of times a string is found within it, and then returns an object based on these results.
Code:
var array = ["Diesel","Asos","Diesel","Paul Smith"];
function frequency(array) {
var frequency = {}, value;
for(var i = 0; i < array.length; i++) {
value = array[i];
if(value in frequency)
frequency[value]++;
else
frequency[value] = 1;
}
return frequency;
}
ordered = frequency(array);
Sample output:
{"Asos":1,"Diesel":2,"Paul Smith":1}
How can I now sort this outputted object based on the counted frequency value?

Categories