JS array value binding to existing array - javascript

Is it possible?
I'm trying to do the following thing:
var foo = 'foo';
var bar = 'bar';
var x;
var y;
var array1 = [x, [foo, y]];
var array2 = [x, [foo, bar], [foo, y]];
console.log(array1[0]); //undefined
console.log(array1[1][1]); //undefined
console.log(array2[0]); //undefined
console.log(array2[2][1]); //undefined
array1[0] = 'working';
array2[0] = 'working';
y = 'hello';
console.log(array1[0]); //working
console.log(array1[1][1]); //undefined
console.log(array2[0]); //working
console.log(array2[2][1]); //undefined
Although it's obvious to define value when you know where the binding value exist ,x in this case, it's hard to bind a value to values with unknown location, y in this case.
Any thoughts? Thanks.

It sounds like what you want is some kind of wrapper:
var x = {};
var y = {};
var array = [x, [foo, y]];
console.log(array[0].value); // undefined
console.log(array[1][1].value); // undefined
x.value = "hello";
y.value = "goodbye";
console.log(array[0].value); // hello
console.log(array[1][1].value); // goodbye

Related

How to copy existing properties of an object to another one in angular?

Consider the following code:
const x={name:"a"};
const y={name:"b", fname:"c"};
const z = Object.assign(x,y); //output: z={name:"b", fname:"c"}
//expected: {name:"b"}
How to achieve the expected result?
You can use for...in loop
const x={name:"a"};
const y={name:"b", fname:"c"};
const z = {};
for (let key in x) z[key] = y.hasOwnProperty(key) ? y[key] : x[key];
console.log(z);
Object.prototype.customAssign = function(x,y){
let item = {};
Object.keys(x).forEach(function(a,b){
destiKeys.indexOf(a)>=0? item[a] = y[a] : null;
});
return item;
};
const x={name:"a"};
const y={name:"b", fname:"c"};
const z = Object.customAssign(x,y); //output: z = {name:"b"}
use spreadOperator https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-1.html
z={...x,..y} //All properties of x + All properties of y
//y properties replace the sames properties of x

Javascript Push/Shift same element multiple times

I have the following code:
var foo = 'foo'
var bar = 'bar'
var arr = [1,2,3]
I want to add to foo several times at the beginning of the array and bar at the end of the array. The number of times each element added should be dynamic and the resulting array should be something like:
['foo','foo',1,2,3,'bar',bar','bar']
Is there a better method than using a loop for each element?I could use lodash if needed.
If better means shorter, yes there's a way:
var foo = 'foo';
var bar = 'bar'
var arr = [1,2,3]
var result = [
...Array(2).fill(foo),
...arr,
...Array(3).fill(bar)
];
Or something like this.
var foo = Array.from({length:2}, v => 'foo');
var bar = Array.from({length:3}, v => 'bar');
var arr = [1,2,3]
arr.push(...bar);
arr.unshift(...foo);
console.log(arr);
Try this forloop method. Array#unshift() added the value on starting of array.push add with end of the array
var foo = 'foo'
var bar = 'bar'
var arr = [1,2,3]
for(var i=0; i<(Math.random() * 5); i++){
arr.unshift(foo)
}
for(var i=0; i<(Math.random() * 5); i++){
arr.push(bar)
}
console.log(arr)
You can use unshift and push like
function pushToBeginning(arr, str, count){
while(count--){
arr.unshift(str);
}
}
function pushToEnd(arr, str, count){
while(count--){
arr.push(str);
}
}
let arr = [1, 2, 3];
pushToBeginning(arr, 'Foo', 3);
pushToEnd(arr, 'Bar', 2);
console.log(arr);

Javascript: Counting each character and storing inside object by property name

LEt's say I have an object like this
var obj = {"a":0, "b":0, "c":0,....., "z":0};
and a string
var str1 = "banana";
How do I get the end result of obj to have
var obj = {"a":3, "b":1,.....,"n":2};
)
You probably don't need that initial object, just use a reducer on the array of characters:
var charFreq = function(x) {
return x.split('').reduce(function(acc, x) {
return acc[x] = ++acc[x] || 1, acc
},{})
}
console.log(charFreq('banana'))
//^ {b:1, a:3, n:2}
If you need the other letters with zero value you can extend an initial object with the result:
var abc = 'abcdefghijklmnopqrstuvwxyz'
.split('')
.reduce(function(a, x){return a[x] = 0, a},{})
var extend = function(x, y) {
Object.keys(y).forEach(function(k){x[k] = y[k]})
return x
}
extend(abc, charFreq('banana'))
console.log(abc)
//^ {a:3, b:1, ..., n:2}

Quick way to globalise a lot of variables in JavaScript?

I need to do a bit of quick testing with my code (getting the value of some variables inside a function), and I want to globalise them, so I can access them through the console.
I know this method:
function foo() {
var foo = 'foo';
window.foo = foo; // Make foo global
}
But what if I had something like this:
function foo() {
var foo1 = 'foo';
var foo2 = 'foo';
var foo3 = 'foo';
var foo4 = 'foo';
var foo5 = 'foo';
var foo6 = 'foo';
var foo7 = 'foo';
var foo8 = 'foo';
}
What would be a quicker way to globalise all those variables, without going window.foo1 = foo1, window.foo2 = foo2, etc.?
I don't wish this to be a code golf question, just a normal programming question.
I don't think there's a way to do this. See this:
Access all local variables
Have you tried simply debugging in the console? With Chrome, you can set a breakpoint and then inspect all values. Check out this tutorial:
https://developers.google.com/chrome-developer-tools/docs/scripts-breakpoints
Why not a single globals object instead of a bunch of variables?
function foo() {
window.globals = {
foo1 = 'foo',
foo2 = 'foo',
foo3 = 'foo',
foo4 = 'foo',
foo5 = 'foo',
foo6 = 'foo',
foo7 = 'foo',
foo8 = 'foo'
};
}
If they're all simply named like that, you can take advantage of a little known trick, variables are actually dictionaries:
function foo() {
var foo1 = 'foo';
var foo2 = 'foo';
var foo3 = 'foo';
var foo4 = 'foo';
var foo5 = 'foo';
var foo6 = 'foo';
var foo7 = 'foo';
var foo8 = 'foo';
for (var i = 1; i <= 8; i++) {
window["foo" + i] = eval("foo" + i);
}
}
document.write("Running foo...<br/>");
foo();
document.write("Printing foo...<br/>");
for (var i = 1; i <= 8; i++) {
document.write(window["foo" + i]);
}
document.write("<br/>Just one: " + foo3);// Normal variable notation

how to match the value of two variables in Jquery

I was just wondering how I can match the value of two variable, for example if I have
var A = [1,2,3];
var b = [A,B,C];
how can I output the first value of each and second value of each and so on, so the output will become
A1,B2,C3
thanks
Using jQuery.map:
var a = ['A','B','C'];
var b = [1,2,3];
var result = $.map(a, function(n, i){
return n + b[i];
}); // ["A1", "B2", "C3"]

Categories