I must reimplement a list and the method forEach with the following instructions:
// Do not construct any array literal ([]) in your solution.
// Do not construct any arrays through new Array in your solution.
// DO not use any of the Array.prototype methods in your solution.
// You may use the destructuring and spreading (...) syntax from Iterable.
the result should look like:
const list = List.create(1, 2)
list.forEach((item) => console.log(item))
Here is my incomplete solution:
export class List {
constuctor(){
}
public static create(...values: number[]): List {
// Do *not* construct any array literal ([]) in your solution.
// Do *not* construct any arrays through new Array in your solution.
// DO *not* use any of the Array.prototype methods in your solution.
// You may use the destructuring and spreading (...) syntax from Iterable.
List list = new List();
values.forEach(function(item){
list.push(item);
});
return list;
}
public forEach(callback: any){
for (let i = 0; i<this.length ; i++){
callback(this[i], i, this);
}
}
}
in the create loop, but the problem, as a static method, the this is not recognized
EDITED thanks to comments
...this is not recognised
It is. But you have not given this any property. And this is because:
constuctor should be written as constructor
You need to define a push method (since you call it in create)
You need to define a length property (since you reference it in forEach)
Furthermore, there some other issues:
you write that Array.prototype functions cannot be used, but your code has values.forEach(), ... so that is a violation of that rule. Use a for..of loop instead.
Here is your code with those remarks taken on board:
class List {
constructor() {
this.length = 0;
}
push(value) {
this[this.length++] = value;
}
static create(...values) {
let list = new List();
for (let item of values) {
list.push(item);
}
return list;
}
forEach(callback) {
for (let i = 0; i < this.length ; i++) {
callback(this[i], i, this);
}
}
}
const list = List.create(1, 2)
list.forEach((item) => console.log(item))
Remarks
The above "test" will be fine, but when also assignments to properties are to work correctly, like list[2] = 3, then there are more things to take care of. Take for instance this program:
const list = List.create(1, 2);
list[5] = 42; // should update length
list.check = true; // should not update length
console.log("length = " + list.length);
console.log("enumerable keys are " + Object.keys(list));
list.forEach((item) => console.log(item)); // should not output empty slots
list.length = 1; // should delete some index properties
list.forEach((item) => console.log(item)); // should not output deleted items
...then the output should be:
length = 6
enumerable keys are 0,1,5,check
1
2
42
1
You can make this happen by trapping the access to properties, and making length a getter/setter. But you'd also need to distinguish between properties that are array indices, and which are not, so all-in-all that would make the code less trivial.
Related
I would like to write a function that will search the fields of objects in an array for a specific string, adding this object to a new array if the string is found in any of the object's fields. I've gotten my function to work, but was having the issue of the new list containing multiple copies of the objects which contain multiple copies of the string being searched for. I know this is because I've looped it so that each field it finds the string in, it will add the object once more to the new array. However, I wasn't sure of what alternative way I could go about writing the function to add the object only once, regardless of how many of its fields have matched with the string being searched for. This is my function:
function search (keyword, array){
var newArray = [];
for(let i = 0; i < array.length; i++) {
for (key in array[i]) {
if(array[i][key].includes(keyword)){
newArray.push(array[i]);
}
}
}
return newArray;
}
An example of where the output is problematic is if I do:
console.log(search('yes', myArray))
And if myArray contains an object in which 'yes' appears in 3 different fields, it will add this object to newArray 3 times.
Improved version of Danny Buonocore code.
No accidental global variables.
Uses forEach to iterate over the array.
Uses for...of and Object.values() to iterate over the values of the object (using for..in iterates over all non-Symbol, enumerable properties of an object itself and those the object inherits from its constructor's prototype and are cause for many bugs)
"short circuit" the test for adding an object: if a value has matched, there is no need to check the other values. This alone would probably solved your problem, but using a set will prevent duplicates if you have the same object multiple times in your array.
function search (keyword, array){
var result = new Set();
array.forEach( object => {
for (const value of Object.values(object)) {
if ( value.includes(keyword) ) {
result.add(object);
continue; // Don't need to check more on this item.
}
}
});
return Array.from(result);
}
console.log(search("yes", [
{ key1 :"yes", key2:"yes" },
{ key1 :"no", key2:"no" }
]));
You could use a Set, this will prevent duplicates.
function search (keyword, array){
var result = new Set();
for(let i = 0; i < array.length; i++) {
for (key in array[i]) {
if(array[i][key].includes(keyword)){
result.add(array[i]);
}
}
}
return Array.from(result);
}
I just noticed this in some code and I've been trying to understand what it is doing.
this.rows[rowIndex][cell] = event.target.value;
this.rows = [...this.rows];
It appears to me that it's simply assigning this.rows to itself. Is there some other use of the spread operator in which this makes sense? Or is it simply a bug?
The spread syntax will give a shallow copy of the original array.
There are at least two reasons why this may be useful:
Any references that exist to the original rows property will not be affected by later assignments made to the direct properties of the copied array.
If the original value of rows was not a true array, but iterable (array-like), then the result of the spread syntax assignment will still be a true array.
Here is an artificially made object to illustrate these two points:
class Foo {
constructor(n) { // Define array-like object
this.rows = {
// Define iterator
[Symbol.iterator]: function* () {
for (let i = 0; i < n; i++) yield this[i];
},
}
// Create "index" properties
for (let i = 0; i < n; i++) this.rows[i] = [];
}
bar(rowIndex, cell, value) {
this.rows[rowIndex][cell] = value;
console.log(this.rows);
// Take shallow copy
this.rows = [...this.rows];
// Now it is a true array
console.log(this.rows);
// We add a value to that array, which other copies will not see
this.rows.push("added");
console.log(this.rows);
}
}
var foo = new Foo(2); // Create array-like object
var remember = foo.rows; // get its rows
foo.bar(1, 0, 15); // set one particular value to 15
// Demonstrate that the value that was added inside the method is not in our copy
console.log(remember);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Note how the first output has the { } notation: it is not recognised as a true array. The second output is with [ ]: a true array. The third output shows an additional value that was pushed on that array. The final output shows these manipulations (making it an array and adding a value to it) are not reflected in the original reference we had to the rows property.
I have an object
currentValues= {hey:1212, git:1212, nmo:12121}
and I use for in like this:
for (const key in currentValues) {
if (Object.prototype.hasOwnProperty.call(currentValues, key)) {
yield put(setCurrentValue(key, currentValues[key]));
}
}
ESLint shows me an error which is saying:
ESLint: for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array. (no-restricted-syntax
How should I edit my code?
It says,
Use Object.{keys,values,entries}, and iterate over the resulting
array.
So you could do something like this to get the object keys as an array and then loop through the keys to make necessary changes.
currentValues= {hey:1212, git:1212, nmo:12121}
Object.keys(currentValues).forEach(function(key) {
yield put(setCurrentValue(key, currentValues[key]));
})
I used the following:
const keys = Object.keys(currentValues);
const values = Object.values(currentValues);
for (let i = 0; i < keys.length; i += 1) {
yield put(setCurrentValue(keys[i], values[i]));
}
This is correct and without ESLint errors.
You can get the array of all your values inside your object just doing
var myValuesInArray = Object.values(currentValues);
I would do it by refactoring, in the following ways.
const currentValues = { hey: 1212, git: 1212, nmo: 12121 };
Object.keys(currentValues).forEach((e) => console.log(`${e} : ${currentValues[e]}`));
Results:
hey : 1212
git : 1212
nmo : 12121
Object.values(currentValues).forEach((e) => console.log(`Values: ${e}`));
Results:
(2)Values: 1212
Values: 12121
Object.entries(currentValues).forEach((e) => console.log(`${e[0]} : ${e[1]}`));
Results:
hey : 1212
git : 1212
nmo : 12121
let animal = {
eats: "Eating",
};
let rabbit = {
jumps: "Jumping",
};
rabbit.__proto__ = animal;
for (const rabbitProps in rabbit) {
// This will print both eats and jumps properties because they're part of
// prototype chain
console.log("rabbitProps: ", rabbitProps);
}
// This prints only jumps as Object.keys shows only the keys of object
console.log(Object.keys(rabbit));
Checking out the above code you can see that when using for...in loop[ it will show all the properties that are part of object as well as properties set in the prototype chain of the object and so eslint suggests to use Object.keys or value or entries, as they do not look into prototype chain instead they just look at your object.
I know it is similar to the above, but here is a full example:
const data = res.data;
const keys = Object.keys(data);
const values = Object.values(data);
for (let i = 0; i <= keys.length; i += 1) {
if (Object.prototype.hasOwnProperty.call(values, i)) {
this.rows.push({
name: values[i].name,
email: values[i].email,
address: values[i].address,
phone: values[i].phone,
role: values[i].role,
});
}
}
try this instead:
Object.keys(currentValues).map(key => (yield put(setCurrentValue(key, currentValues[key]))));
Using for...in will iterate over all properties including those from Object prototype. I am not sure why you are doing Object.prototype.hasOwnProperty.call(currentValues, key)
instead of just:
currentValues.hasOwnProperty(key).
I think this should make ESLint aware that you are filtering for own properties only.
However, I suggest using for (const key of Object.keys()), which is more semantic.
Javascript has arrays which use numeric indexes ["john", "Bob", "Joe"] and objects which can be used like associative arrays or "maps" that allow string keys for the object values {"john" : 28, "bob": 34, "joe" : 4}.
In PHP it is easy to both A) sort by values (while maintaining the key) and B) test for the existence of a value in an associative array.
$array = ["john" => 28, "bob" => 34, "joe" => 4];
asort($array); // ["joe" => 4, "john" => 28, "bob" => 34];
if(isset($array["will"])) { }
How would you acheive this functionality in Javascript?
This is a common need for things like weighted lists or sorted sets where you need to keep a single copy of a value in data structure (like a tag name) and also keep a weighted value.
This is the best I've come up with so far:
function getSortedKeys(obj) {
var keys = Object.keys(obj);
keys = keys.sort(function(a,b){return obj[a]-obj[b]});
var map = {};
for (var i = keys.length - 1; i >= 0; i--) {
map[keys[i]] = obj[keys[i]];
};
return map;
}
var list = {"john" : 28, "bob": 34, "joe" : 4};
list = getSortedKeys(list);
if(list["will"]) { }
Looking at this answer by Luke Schafer I think I might have found a better way to handle this by extending the Object.prototype:
// Sort by value while keeping index
Object.prototype.iterateSorted = function(worker, limit)
{
var keys = Object.keys(this), self = this;
keys.sort(function(a,b){return self[b] - self[a]});
if(limit) {
limit = Math.min(keys.length, limit);
}
limit = limit || keys.length;
for (var i = 0; i < limit; i++) {
worker(keys[i], this[keys[i]]);
}
};
var myObj = { e:5, c:3, a:1, b:2, d:4, z:1};
myObj.iterateSorted(function(key, value) {
console.log("key", key, "value", value)
}, 3);
http://jsfiddle.net/Xeoncross/kq3gbwgh/
With ES6 you could choose to extend the Map constructor/class with a sort method that takes an optional compare function (just like arrays have). That sort method would take two arguments, each of which are key/value pairs so that the sorting can happen on either the keys or the values (or both).
The sort method will rely on the documented behaviour of Maps that entries are iterated in insertion order. So this new method will visit the entries according to the sorted order, and then delete and immediately re-insert them.
Here is how that could look:
class SortableMap extends Map {
sort(cmp = (a, b) => a[0].localeCompare(b[0])) {
for (const [key, value] of [...this.entries()].sort(cmp)) {
this.delete(key);
this.set(key, value); // New keys are added at the end of the order
}
}
}
// Demo
const mp = new SortableMap([[3, "three"],[1, "one"],[2, "two"]]);
console.log("Before: ", JSON.stringify([...mp])); // Before
mp.sort( (a, b) => a[0] - b[0] ); // Custom compare function: sort numerical keys
console.log(" After: ", JSON.stringify([...mp])); // After
I'm not sure why none of these answers mentions the existence of a built-in JS class, Set. Seems to be an ES6 addition, perhaps that's why.
Ideally override either add or keys below... NB overriding keys doesn't even need access to the Set object's prototype. Of course you could override these methods for the entire Set class. Or make a subclass, SortedSet.
const mySet = new Set();
const mySetProto = Object.getPrototypeOf(mySet);
const addOverride = function(newObj){
const arr = Array.from(this);
arr.add(newObj);
arr.sort(); // or arr.sort(function(a, b)...)
this.clear();
for(let item of arr){
mySetProto.add.call(this, item);
}
}
mySet.add = addOverride;
const keysOverride = function(){
const arr = Array.from(this);
arr.sort(); // or arr.sort(function(a, b)...)
return arr[Symbol.iterator]();
}
mySet.keys = keysOverride;
Usage:
mySet.add(3); mySet.add(2); mySet.add(1); mySet.add(2);
for(let item of mySet.keys()){console.log(item)};
Prints out:
1 ... 2 ... 3
NB Set.keys() returns not the items in the Set, but an iterator. You could choose to return the sorted array instead, but you'd obviously be breaking the class's "contract".
Which one to override? Depends on your usage and the size of your Set. If you override both you will be duplicating the sort activity, but in most cases it probably won't matter.
NB The add function I suggest is of course naive, a "first draft": rebuilding the entire set each time you add could be pretty costly. There are clearly much cleverer ways of doing this based on examining the existing elements in the Set and using a compare function, a binary tree structure*, or some other method to determine where in it to add the candidate for adding (I say "candidate" because it would be rejected if an "identical" element, namely itself, were already found to be present).
The question also asks about similar arrangements for a sorted map... in fact it turns out that ES6 has a new Map class which lends itself to similar treatment ... and also that Set is just a specialised Map, as you might expect.
* e.g. https://github.com/Crizstian/data-structure-and-algorithms-with-ES6/tree/master/10-chapter-Binary-Tree
You usually don't sort an object. But if you do: Sorting JavaScript Object by property value
If you want to sort an array, let's say the following
var arraylist = [{"john" : 28},{ "bob": 34},{ "joe" : 4}];
You can always use Array.prototype.sort function.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Maybe this code look like what you want:
Object.prototype.asort = function(){
var retVal = {};
var self = this;
var keys = Object.keys(this);
keys = keys.sort(function(a,b){return self[a] - self[b]});
for (var i = 0; i < keys.length; i++) {
retVal[keys[i]] = this[keys[i]];
}
return retVal;
}
var map = {"john" : 28, "bob": 34, "joe" : 4}
var sortedMap = map.asort();//sortedMap["will"]: undefined
If you use the open source project jinqJs its easy.
See Fiddler
var result = jinqJs()
.from([{"john" : 28},{ "bob": 34},{ "joe" : 4}])
.orderBy([{field: 0}])
.select();
Here's an implementation of OrderedMap.
Use the functions get() and set() to extract or push key value pairs to the OrderedMap.
It is internally using an array to maintain the order.
class OrderedMap {
constructor() {
this.arr = [];
return this;
}
get(key) {
for(let i=0;i<this.arr.length;i++) {
if(this.arr[i].key === key) {
return this.arr[i].value;
}
}
return undefined;
}
set(key, value) {
for(let i=0;i<this.arr.length;i++) {
if(this.arr[i].key === key) {
this.arr[i].value = value;
return;
}
}
this.arr.push({key, value})
}
values() {
return this.arr;
}
}
let m = new OrderedMap();
m.set('b', 60)
m.set('a', 10)
m.set('c', 20)
m.set('d', 89)
console.log(m.get('a'));
console.log(m.values());
https://github.com/js-sdsl/js-sdsl
The OrderedMap in Js-sdsl maybe helpful.
This is a sorted-map which implement refer to C++ STL Map.
/*
* key value
* 1 1
* 2 2
* 3 3
* Sorted by key.
*/
const mp = new OrderedMap(
[1, 2, 3].map((element, index) => [index, element])
);
mp.setElement(1, 2); // O(logn)
mp.eraseElementByKey(1) // O(logn)
// custom comparison function
mp = new OrderedMap(
[1, 2, 3].map((element, index) => [index, element]),
(x, y) => x - y
);
// enable tree iterator index (enableIndex = true)
console.log(new OrderedMap([[0, 1], [1, 1]], undefined, true).begin(),next().index); // 1
Considering this data structure:
var vehicles = [
[ "2011","Honda","Accord" ],
[ "2010","Honda","Accord" ],
.....
];
Looping through each vehicles item, is there a way to reassign the array elements to individual variables all in one shot, something like:
for (i = 0; i < vehicles.length; i++) {
var(year,make,model) = vehicles[i]; // doesn't work
.....
}
... I'm trying to get away from doing:
for (i = 0; i < vehicles.length; i++) {
var year = vehicles[i][0];
var make = vehicles[i][1];
var model = vehicles[i][2];
.....
}
Just curious since this type of thing is available in other programming languages. Thanks!
Now it is possible using ES6's Array Destructuring.
As from Docs:
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Consider the following example:
let [a, b, c] = [10, 20, 30];
console.log(a); // output => 10
console.log(b); // output => 20
console.log(c); // output => 30
As with your data, .forEach() method can also be used for iterating over array elements along with Array Destructuring:
let vehicles = [
[ "2011","Honda","Accord" ],
[ "2010","Honda","Accord" ]
];
vehicles.forEach(([year, make, model], index) => {
// ... your code here ...
console.log(`${year}, ${make}, ${model}, ${index}`);
});
References:
Array Destructuring
Array.prototype.forEach()
Arrow Functions
Template Literals
No unfortunately there is not a method to do this currently XBrowser. (that I'm aware of).
Relatively soon it's possible cross browser, see link:
https://developer.mozilla.org/en/New_in_JavaScript_1.7
(In PHP there is "list" which will do exactly what you wish, nothing similar XBrowser for javascript yet)
Of course relatively soon could mean anything etc. (Thanks Felix for pointing out my errors in this)
edit: This is now available see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring
Probably the closest you'll currently get in javascript is to eliminate the redundant var and separate the statements with a comma separator.
for (i = 0; i < vehicles.length; i++) {
var year = vehicles[i][0], make = vehicles[i][1], model = vehicles[i][2];
.....
}
or you could shorten it a bit more like this:
for (i = 0; i < vehicles.length; i++) {
var v = vehicles[i], year = v[0], make = v[1], model = v[2];
.....
}
The closest alternative that I could think of is using a function and using apply() to call it. Passing an array, it would get passed as each argument.
function vehicle(year, make, model) {
// do stuff
}
for (i = 0; i < vehicles.length; i++) {
vehicle.apply (this, vehicles[i]);
}
Or an anonymous function:
for (i = 0; i < vehicles.length; i++) {
(function(year, make, model) {
// do stuff
}).apply(this, vehicles[i]);
}
Unpacking array into separate variables in JavaScript
The destructuring assignment syntax is a JavaScript expression that
makes it possible to unpack values from arrays, or properties from objects,into distinct variables.
let array = [2,3];
[a,b] = array;// unpacking array into var a and b
console.log(a); //output 2
console.log(b); //output 3
let obj = {name:"someone",weight:"500pounds"};
let {name,weight} = obj; // unpacking obj into var name and weight
console.log(name);// output someone
console.log(weight);//output 500pounds
Source