Double question javascript functions with arrays - javascript

This is a double question because I can just post once every 90 minutes.
First I have to write a function that replaces a character of a string.
//====================== EXAMPLE ========================
var str = "I,Really,Like,Pizza";
characterRemover(str, ",");
"I Really Like Pizza"; // <====== EXPECTED OUTPUT
//=========================================================
And puts a space in place of the chosen character. I tried this but is not working.
function chracterRemover(str, cha){
var replaced = str.split('cha').join(' ');
return replaced;
}
It returns just the same string.
And the second thing is that I have to write a function that returns true if the data type introduced is an arrat and false for the rest.
//====================== EXAMPLE ========================
var one = { name: "antonello" };
false; // <====== EXPECTED OUTPUT
var two = ["name", "antonello"];
true; // <====== EXPECTED OUTPUT
var three = [[], [], {}, "antonello", 3, function() {}];
true; // <====== EXPECTED OUTPUT
//=========================================================
I've tried this.
function isArrayFun(array){
if {
typeof array = 'array';
return "Array";
} else {
return "Not an array"
}
}
But as well, it doesnt work.
I get this error:
Uncaught SyntaxError: Unexpected token '{'
I don't know why.
Thanks in advance for the help.

// First One
const str = "I,Really,Like,Pizza";
console.log(str.split(',').join(' '));
// Second One
function isArrayFun(array){
return Array.isArray(array);
}
const one = { name: "antonello" };
console.log(isArrayFun(one));
const two = ["name", "antonello"];
console.log(isArrayFun(two));
const three = [[], [], {}, "antonello", 3, function() {}];
console.log(isArrayFun(three));

Problem 1:
You quoted cha so it's a literal string, not the variable. Use
function characterRemover(str, cha) {
var replaced = str.split(cha).join(' ');
return replaced;
}
var str = "I,Really,Like,Pizza";
console.log(characterRemover(str, ","));
Problem 2:
typeof returns object for arrays. The way to tell if something is an array is by calling Array.isArray().
You also have syntax errors in your if statement. The condition has to be inside (), not after {.
function isArrayFun(array) {
if (Array.isArray(array)) {
return "Array";
} else {
return "Not an array"
}
}
var one = { name: "antonello" };
console.log(isArrayFun(one));
var two = ["name", "antonello"];
console.log(isArrayFun(two));
var three = [[], [], {}, "antonello", 3, function() {}];
console.log(isArrayFun(three));

First question.
The function name is different than the function you called
you should use .split(cha) and not 'cha'. split cha will actually split your string by the string you passed into that parameter. And 'cha' looks for the string 'cha'
Working example:
var str = "I,Really,Like,Pizza";
function chracterRemover(str, cha){
var replaced = str.split(cha).join(' ');
return replaced;
}
console.log(chracterRemover(str, ","));
You could also use a simple RegExp instead of using split and join and take the function to another level by making it globally useful via a 3rd parameter, in which you could define the replacement:
var str = "I,Really,Like,Pizza";
function chracterRemover(str, cha, chaTo){
var reg = new RegExp(cha, "g");
return str.replace(reg, chaTo);
}
console.log(chracterRemover(str, ",", " "));
console.log(chracterRemover(str, ",", "."));
Second question:
There is already a function like that
Array.isArray(value)
you can pass any type of data into that value, if it is an array it returns true
working example:
let type1 = [1,5,6];
let type2 = {a: 47};
let type3 = 5;
let type4 = "hello";
console.log(Array.isArray(type1))
console.log(Array.isArray(type2))
console.log(Array.isArray(type3))
console.log(Array.isArray(type4))

Related

Is it possible to convert a double quoted string with template literal to tilde string to assign values? [duplicate]

Is it possible to create a template string as a usual string,
let a = "b:${b}";
and then convert it into a template string,
let b = 10;
console.log(a.template()); // b:10
without eval, new Function and other means of dynamic code generation?
In my project I've created something like this with ES6:
String.prototype.interpolate = function(params) {
const names = Object.keys(params);
const vals = Object.values(params);
return new Function(...names, `return \`${this}\`;`)(...vals);
}
const template = 'Example text: ${text}';
const result = template.interpolate({
text: 'Foo Boo'
});
console.log(result);
As your template string must get reference to the b variable dynamically (in runtime), so the answer is: NO, it's impossible to do it without dynamic code generation.
But, with eval it's pretty simple:
let tpl = eval('`'+a+'`');
No, there is not a way to do this without dynamic code generation.
However, I have created a function which will turn a regular string into a function which can be provided with a map of values, using template strings internally.
Generate Template String Gist
/**
* Produces a function which uses template strings to do simple interpolation from objects.
*
* Usage:
* var makeMeKing = generateTemplateString('${name} is now the king of ${country}!');
*
* console.log(makeMeKing({ name: 'Bryan', country: 'Scotland'}));
* // Logs 'Bryan is now the king of Scotland!'
*/
var generateTemplateString = (function(){
var cache = {};
function generateTemplate(template){
var fn = cache[template];
if (!fn){
// Replace ${expressions} (etc) with ${map.expressions}.
var sanitized = template
.replace(/\$\{([\s]*[^;\s\{]+[\s]*)\}/g, function(_, match){
return `\$\{map.${match.trim()}\}`;
})
// Afterwards, replace anything that's not ${map.expressions}' (etc) with a blank string.
.replace(/(\$\{(?!map\.)[^}]+\})/g, '');
fn = Function('map', `return \`${sanitized}\``);
}
return fn;
}
return generateTemplate;
})();
Usage:
var kingMaker = generateTemplateString('${name} is king!');
console.log(kingMaker({name: 'Bryan'}));
// Logs 'Bryan is king!' to the console.
Hope this helps somebody. If you find a problem with the code, please be so kind as to update the Gist.
What you're asking for here:
//non working code quoted from the question
let b=10;
console.log(a.template());//b:10
is exactly equivalent (in terms of power and, er, safety) to eval: the ability to take a string containing code and execute that code; and also the ability for the executed code to see local variables in the caller's environment.
There is no way in JS for a function to see local variables in its caller, unless that function is eval(). Even Function() can't do it.
When you hear there's something called "template strings" coming to JavaScript, it's natural to assume it's a built-in template library, like Mustache. It isn't. It's mainly just string interpolation and multiline strings for JS. I think this is going to be a common misconception for a while, though. :(
There are many good solutions posted here, but none yet which utilizes the ES6 String.raw method. Here is my contriubution. It has an important limitation in that it will only accept properties from a passed in object, meaning no code execution in the template will work.
function parseStringTemplate(str, obj) {
let parts = str.split(/\$\{(?!\d)[\wæøåÆØÅ]*\}/);
let args = str.match(/[^{\}]+(?=})/g) || [];
let parameters = args.map(argument => obj[argument] || (obj[argument] === undefined ? "" : obj[argument]));
return String.raw({ raw: parts }, ...parameters);
}
let template = "Hello, ${name}! Are you ${age} years old?";
let values = { name: "John Doe", age: 18 };
parseStringTemplate(template, values);
// output: Hello, John Doe! Are you 18 years old?
Split string into non-argument textual parts. See regex.
parts: ["Hello, ", "! Are you ", " years old?"]
Split string into property names. Empty array if match fails.
args: ["name", "age"]
Map parameters from obj by property name. Solution is limited by shallow one level mapping. Undefined values are substituted with an empty string, but other falsy values are accepted.
parameters: ["John Doe", 18]
Utilize String.raw(...) and return result.
TLDR:
https://jsfiddle.net/bj89zntu/1/
Everyone seems to be worried about accessing variables. Why not just pass them? I'm sure it won't be too hard to get the variable context in the caller and pass it down. Use
ninjagecko's answer to get the props from obj.
function renderString(str,obj){
return str.replace(/\$\{(.+?)\}/g,(match,p1)=>{return index(obj,p1)})
}
Here is the full code:
function index(obj,is,value) {
if (typeof is == 'string')
is=is.split('.');
if (is.length==1 && value!==undefined)
return obj[is[0]] = value;
else if (is.length==0)
return obj;
else
return index(obj[is[0]],is.slice(1), value);
}
function renderString(str,obj){
return str.replace(/\$\{.+?\}/g,(match)=>{return index(obj,match)})
}
renderString('abc${a}asdas',{a:23,b:44}) //abc23asdas
renderString('abc${a.c}asdas',{a:{c:22,d:55},b:44}) //abc22asdas
The issue here is to have a function that has access to the variables of its caller. This is why we see direct eval being used for template processing. A possible solution would be to generate a function taking formal parameters named by a dictionary's properties, and calling it with the corresponding values in the same order. An alternative way would be to have something simple as this:
var name = "John Smith";
var message = "Hello, my name is ${name}";
console.log(new Function('return `' + message + '`;')());
And for anyone using Babel compiler we need to create closure which remembers the environment in which it was created:
console.log(new Function('name', 'return `' + message + '`;')(name));
I liked s.meijer's answer and wrote my own version based on his:
function parseTemplate(template, map, fallback) {
return template.replace(/\$\{[^}]+\}/g, (match) =>
match
.slice(2, -1)
.trim()
.split(".")
.reduce(
(searchObject, key) => searchObject[key] || fallback || match,
map
)
);
}
Similar to Daniel's answer (and s.meijer's gist) but more readable:
const regex = /\${[^{]+}/g;
export default function interpolate(template, variables, fallback) {
return template.replace(regex, (match) => {
const path = match.slice(2, -1).trim();
return getObjPath(path, variables, fallback);
});
}
//get the specified property or nested property of an object
function getObjPath(path, obj, fallback = '') {
return path.split('.').reduce((res, key) => res[key] || fallback, obj);
}
Note: This slightly improves s.meijer's original, since it won't match things like ${foo{bar} (the regex only allows non-curly brace characters inside ${ and }).
UPDATE: I was asked for an example using this, so here you go:
const replacements = {
name: 'Bob',
age: 37
}
interpolate('My name is ${name}, and I am ${age}.', replacements)
#Mateusz Moska, solution works great, but when i used it in React Native(build mode), it throws an error: Invalid character '`', though it works when i run it in debug mode.
So i wrote down my own solution using regex.
String.prototype.interpolate = function(params) {
let template = this
for (let key in params) {
template = template.replace(new RegExp('\\$\\{' + key + '\\}', 'g'), params[key])
}
return template
}
const template = 'Example text: ${text}',
result = template.interpolate({
text: 'Foo Boo'
})
console.log(result)
Demo: https://es6console.com/j31pqx1p/
NOTE: Since I don't know the root cause of an issue, i raised a ticket in react-native repo, https://github.com/facebook/react-native/issues/14107, so that once they can able to fix/guide me about the same :)
You can use the string prototype, for example
String.prototype.toTemplate=function(){
return eval('`'+this+'`');
}
//...
var a="b:${b}";
var b=10;
console.log(a.toTemplate());//b:10
But the answer of the original question is no way.
I required this method with support for Internet Explorer. It turned out the back ticks aren't supported by even IE11. Also; using eval or it's equivalent Function doesn't feel right.
For the one that notice; I also use backticks, but these ones are removed by compilers like babel. The methods suggested by other ones, depend on them on run-time. As said before; this is an issue in IE11 and lower.
So this is what I came up with:
function get(path, obj, fb = `$\{${path}}`) {
return path.split('.').reduce((res, key) => res[key] || fb, obj);
}
function parseTpl(template, map, fallback) {
return template.replace(/\$\{.+?}/g, (match) => {
const path = match.substr(2, match.length - 3).trim();
return get(path, map, fallback);
});
}
Example output:
const data = { person: { name: 'John', age: 18 } };
parseTpl('Hi ${person.name} (${person.age})', data);
// output: Hi John (18)
parseTpl('Hello ${person.name} from ${person.city}', data);
// output: Hello John from ${person.city}
parseTpl('Hello ${person.name} from ${person.city}', data, '-');
// output: Hello John from -
I currently can't comment on existing answers so I am unable to directly comment on Bryan Raynor's excellent response. Thus, this response is going to update his answer with a slight correction.
In short, his function fails to actually cache the created function, so it will always recreate, regardless of whether it's seen the template before. Here is the corrected code:
/**
* Produces a function which uses template strings to do simple interpolation from objects.
*
* Usage:
* var makeMeKing = generateTemplateString('${name} is now the king of ${country}!');
*
* console.log(makeMeKing({ name: 'Bryan', country: 'Scotland'}));
* // Logs 'Bryan is now the king of Scotland!'
*/
var generateTemplateString = (function(){
var cache = {};
function generateTemplate(template){
var fn = cache[template];
if (!fn){
// Replace ${expressions} (etc) with ${map.expressions}.
var sanitized = template
.replace(/\$\{([\s]*[^;\s\{]+[\s]*)\}/g, function(_, match){
return `\$\{map.${match.trim()}\}`;
})
// Afterwards, replace anything that's not ${map.expressions}' (etc) with a blank string.
.replace(/(\$\{(?!map\.)[^}]+\})/g, '');
fn = cache[template] = Function('map', `return \`${sanitized}\``);
}
return fn;
};
return generateTemplate;
})();
Still dynamic but seems more controlled than just using a naked eval:
const vm = require('vm')
const moment = require('moment')
let template = '### ${context.hours_worked[0].value} \n Hours worked \n #### ${Math.abs(context.hours_worked_avg_diff[0].value)}% ${fns.gt0(context.hours_worked_avg_diff[0].value, "more", "less")} than usual on ${fns.getDOW(new Date())}'
let context = {
hours_worked:[{value:10}],
hours_worked_avg_diff:[{value:10}],
}
function getDOW(now) {
return moment(now).locale('es').format('dddd')
}
function gt0(_in, tVal, fVal) {
return _in >0 ? tVal: fVal
}
function templateIt(context, template) {
const script = new vm.Script('`'+template+'`')
return script.runInNewContext({context, fns:{getDOW, gt0 }})
}
console.log(templateIt(context, template))
https://repl.it/IdVt/3
I made my own solution doing a type with a description as a function
export class Foo {
...
description?: Object;
...
}
let myFoo:Foo = {
...
description: (a,b) => `Welcome ${a}, glad to see you like the ${b} section`.
...
}
and so doing:
let myDescription = myFoo.description('Bar', 'bar');
I came up with this implementation and it works like a charm.
function interpolateTemplate(template: string, args: any): string {
return Object.entries(args).reduce(
(result, [arg, val]) => result.replace(`$\{${arg}}`, `${val}`),
template,
)
}
const template = 'This is an example: ${name}, ${age} ${email}'
console.log(interpolateTemplate(template,{name:'Med', age:'20', email:'example#abc.com'}))
You could raise an error if arg is not found in template
This solution works without ES6:
function render(template, opts) {
return new Function(
'return new Function (' + Object.keys(opts).reduce((args, arg) => args += '\'' + arg + '\',', '') + '\'return `' + template.replace(/(^|[^\\])'/g, '$1\\\'') + '`;\'' +
').apply(null, ' + JSON.stringify(Object.keys(opts).reduce((vals, key) => vals.push(opts[key]) && vals, [])) + ');'
)();
}
render("hello ${ name }", {name:'mo'}); // "hello mo"
Note: the Function constructor is always created in the global scope, which could potentially cause global variables to be overwritten by the template, e.g. render("hello ${ someGlobalVar = 'some new value' }", {name:'mo'});
You should try this tiny JS module, by Andrea Giammarchi, from github :
https://github.com/WebReflection/backtick-template
/*! (C) 2017 Andrea Giammarchi - MIT Style License */
function template(fn, $str, $object) {'use strict';
var
stringify = JSON.stringify,
hasTransformer = typeof fn === 'function',
str = hasTransformer ? $str : fn,
object = hasTransformer ? $object : $str,
i = 0, length = str.length,
strings = i < length ? [] : ['""'],
values = hasTransformer ? [] : strings,
open, close, counter
;
while (i < length) {
open = str.indexOf('${', i);
if (-1 < open) {
strings.push(stringify(str.slice(i, open)));
open += 2;
close = open;
counter = 1;
while (close < length) {
switch (str.charAt(close++)) {
case '}': counter -= 1; break;
case '{': counter += 1; break;
}
if (counter < 1) {
values.push('(' + str.slice(open, close - 1) + ')');
break;
}
}
i = close;
} else {
strings.push(stringify(str.slice(i)));
i = length;
}
}
if (hasTransformer) {
str = 'function' + (Math.random() * 1e5 | 0);
if (strings.length === values.length) strings.push('""');
strings = [
str,
'with(this)return ' + str + '([' + strings + ']' + (
values.length ? (',' + values.join(',')) : ''
) + ')'
];
} else {
strings = ['with(this)return ' + strings.join('+')];
}
return Function.apply(null, strings).apply(
object,
hasTransformer ? [fn] : []
);
}
template.asMethod = function (fn, object) {'use strict';
return typeof fn === 'function' ?
template(fn, this, object) :
template(this, fn);
};
Demo (all the following tests return true):
const info = 'template';
// just string
`some ${info}` === template('some ${info}', {info});
// passing through a transformer
transform `some ${info}` === template(transform, 'some ${info}', {info});
// using it as String method
String.prototype.template = template.asMethod;
`some ${info}` === 'some ${info}'.template({info});
transform `some ${info}` === 'some ${info}'.template(transform, {info});
Faz assim (This way):
let a = 'b:${this.b}'
let b = 10
function template(templateString, templateVars) {
return new Function('return `' + templateString + '`').call(templateVars)
}
result.textContent = template(a, {b})
<b id=result></b>
Since we're reinventing the wheel on something that would be a lovely feature in javascript.
I use eval(), which is not secure, but javascript is not secure. I readily admit that I'm not excellent with javascript, but I had a need, and I needed an answer so I made one.
I chose to stylize my variables with an # rather than an $, particularly because I want to use the multiline feature of literals without evaluating til it's ready. So variable syntax is #{OptionalObject.OptionalObjectN.VARIABLE_NAME}
I am no javascript expert, so I'd gladly take advice on improvement but...
var prsLiteral, prsRegex = /\#\{(.*?)(?!\#\{)\}/g
for(i = 0; i < myResultSet.length; i++) {
prsLiteral = rt.replace(prsRegex,function (match,varname) {
return eval(varname + "[" + i + "]");
// you could instead use return eval(varname) if you're not looping.
})
console.log(prsLiteral);
}
A very simple implementation follows
myResultSet = {totalrecords: 2,
Name: ["Bob", "Stephanie"],
Age: [37,22]};
rt = `My name is #{myResultSet.Name}, and I am #{myResultSet.Age}.`
var prsLiteral, prsRegex = /\#\{(.*?)(?!\#\{)\}/g
for(i = 0; i < myResultSet.totalrecords; i++) {
prsLiteral = rt.replace(prsRegex,function (match,varname) {
return eval(varname + "[" + i + "]");
// you could instead use return eval(varname) if you're not looping.
})
console.log(prsLiteral);
}
In my actual implementation, I choose to use #{{variable}}. One more set of braces. Absurdly unlikely to encounter that unexpectedly. The regex for that would look like /\#\{\{(.*?)(?!\#\{\{)\}\}/g
To make that easier to read
\#\{\{ # opening sequence, #{{ literally.
(.*?) # capturing the variable name
# ^ captures only until it reaches the closing sequence
(?! # negative lookahead, making sure the following
# ^ pattern is not found ahead of the current character
\#\{\{ # same as opening sequence, if you change that, change this
)
\}\} # closing sequence.
If you're not experienced with regex, a pretty safe rule is to escape every non-alphanumeric character, and don't ever needlessly escape letters as many escaped letters have special meaning to virtually all flavors of regex.
You can refer to this solution
const interpolate = (str) =>
new Function(`return \`${new String(str)}\`;`)();
const foo = 'My';
const obj = {
text: 'Hanibal Lector',
firstNum: 1,
secondNum: 2
}
const str = "${foo} name is : ${obj.text}. sum = ${obj.firstNum} + ${obj.secondNum} = ${obj.firstNum + obj.secondNum}";
console.log(interpolate(str));
I realize I am late to the game, but you could:
const a = (b) => `b:${b}`;
let b = 10;
console.log(a(b)); // b:10

How to write a function that takes a string and object and interpolates that object in the string?

Hi everyone I need to write a function that takes a string and object and interpolates that object in the string so something like this
// interpolate("Its {weather} outside", {weather: 'damn Hot'})
// returns 'It's damn hot outside'
// interpolate( "Hi my name is {name}", {name: 'John'});
// returns 'Hi my name is John'
It should also no matter how deep the object goes so a case like this
// interpolate("Hi my name is {contact.name}", {contact: {name: 'john'}});
Im a little stuck and at first I tried splitting up the string into an array then trying to fit the object value in the array then join that back together but that has not worked for all test cases, can someone help me write this function and explain their solution please? Thankyou
so I tried something like this but does not really work for all test cases, this is a template literal but the function should just work giving those values as arguments on its own, otherwise I'm pretty stuck . .
function interpolate(strings, ...keys) {
return (function(...values) {
var dict = values[values.length - 1] || {};
var result = [strings[0]];
keys.forEach(function(key, i) {
var value = Number.isInteger(key) ? values[key] : dict[key];
result.push(value, strings[i + 1]);
});
return result.join('');
});
}
var t1Closure = interpolate`${0}${1}${0}!`;
t1Closure('Y', 'A'); // "YAY!"
var t2Closure = interpolate`${0} ${'foo'}!`;
console.log(t2Closure('Hello', {foo: 'World'})); // "Hello World!"
ok I'm getting closer so I separated the problem into two functions and need to combine them, the only thing is I'm not sure how to get the last use case to work without template literals
var something = "something";
var sub = function(str) {
return str.replace(/#\{(.*?)\}/g,
function(whole, expr) {
return eval(expr)
})
}
console.log(sub("hello #{something}"));
var objectValue = function(str, obj){
for(key in obj) {
if(obj.hasOwnProperty(key)) {
var value = obj[key];
return str + value;
}
}
}
console.log(objectValue("hi my name is ", {contact: {name: 'john'}}));
Using the dreaded eval
If you control the string that you pass and consider it safe, you can use eval:
function interpolate (str, obj) {
return str.replace(/\{(.*?)\}/g, function (_, ref) {
return eval('obj.' + ref);
});
}
var output = interpolate("Its {weather} outside", {weather: 'damn Hot'});
console.log(output);
output = interpolate("Hi my name is {contact.name}", {contact: {name: 'john'}});
console.log(output);
output = interpolate("The highest bid is {rank[0].bid}", {rank: [{bid: 900}, {bid:600}]});
console.log(output);
Be aware that if given a string like '{x;alert("hi")}', the above function would execute that alert, or any code that is put instead. So this is not a good solution if the string is provided (or can be altered) by the user.
Template Literals
It does not follow your function descriptor, but template literals already offer the functionality you are looking for:
var weather = 'damn Hot';
var output = `It's ${weather} outside`;
console.log(output);
var contact = {name: 'john'};
var output = `Hi my name is ${contact.name}`;
console.log(output);
Here you go:
'use strict';
function interpolate(str, obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
str = str.replace(new RegExp(`{${prop}}`, 'g'), obj[prop]);
}
}
return str;
}
console.log(interpolate("Its {weather} outside", {weather: 'damn Hot'}));
// Its damn Hot outside
Try renderjs.
$("<p>Its {{:weather}} outside</p>").render({weather: 'damn Hot'})
-> <p>Its damn Hot outside</p>
Or lodash templates. Lodash already has a lot of handy features you end up using a lot of in my opinion.
var str = _.template('Its <%= weather %> outside')({weather: 'damn Hot'});
// If you wanted to use the {{:var}} syntax.
_.templateSettings.interpolate = /{{:([\s\S]+?)}}/g;
var str = _.template('Its {{:weather}} outside')({weather: 'damn Hot'});
A little late to the party, but in case you cannot (don't want to) use any external libraries or ES6, here is the idea:
function getMatches(s) {
var regExp = /{([^}]*)}/g,
matches = [],
match;
while ((match = regExp.exec(s)) != null) {
matches.push(match[1]);
}
return matches;
}
function objValue(obj, i) {
return obj[i];
}
function interpolate(s, obj) {
var matches = getMatches(s),
result = s;
matches.forEach(function (match) {
var props = match.split('.'),
value = props.reduce(objValue, obj) || '';
result = result.replace('{' + match + '}', value);
});
return result;
}
Usage
interpolate("Its {weather} outside", { weather: 'damn Hot' });
JSFiddle.

Select global variable by string, including sub-properties

In javascript, I want to select a certain property by it's name, which is stored in a string. I know that window[someString] is the way to go. It works when someString = "somevariable", but unfortunately, my program will also have strings such as someobject.someproperty. This does not work.
So the question is, given code
someString = "one.two.three";
one = {
two: {
three: "This is the value that I want to get"
}
};
// window[someString] does not work.
, how can I get the value of one.two.three using the value of someString, without using eval?
Use split and make a recursive method
var someString = "one.two.three";
var keys = someString.split('.');
one = {
two: {
three: "This is the value that I want to get"
}
};
function getinnerProperty(object, keys) {
var key = keys.shift();
if (keys.length) {
var nestedObject = object[key];
return getinnerProperty(nestedObject, keys);
} else {
return object[key];
}
}
console.log(getinnerProperty(window, keys));
You can write a function that uses split and iteratively traverses the object tree:
var someString = "one.two.three";
var one = {
two: {
three: "This is the value that I want to get"
}
};
function getValue(keyStr) {
var keys = keyStr.split('.');
var result = global;
for (var i = 0; i < keys.length; i++) {
result = result[keys[i]];
}
return result != global ? result : undefined;
}
getValue(someString);

Why doesn't the below code log "onetwo" as expected?

We have the function
function Buffer(initValue) {
this.append(initValue);
}
Buffer.prototype = {
items: [],
append: function(str) {
this.items[this.items.length] = str instanceof Buffer ? str.toString() : str;
return this;
},
toString: function() {
return this.items.join("");
}
};
console.log(new Buffer("one").append(new Buffer("two")).toString());
but suddenly it logs "onetwoonetwo" - beyond all expectations... Why?
Several answers explain why this happens in your specific instance.
I suspect that what you really want is to let items be local to each instance of Buffer instead of shared via the prototype. This is simply done by declaring items = []; in the constructor:
function Buffer(initValue) {
this.items = []; // This line replaces `items: [],` in the original code.
this.append(initValue);
}
Buffer.prototype = {
append: function(str) {
this.items[this.items.length] = str instanceof Buffer ? str.toString() : str;
return this;
},
toString: function() {
return this.items.join("");
}
};
This is because you applied append multiple time.
Since you are using prototype, your item array is shared by your two instance of Buffer.
You should have this: http://jsbin.com/luhoki/3/watch?html,css,js,output
function Buffer(initValue) {
this.append(initValue);
}
Buffer.prototype = {
items: [],
append: function(str) {
this.items[this.items.length] = str instanceof Buffer ? str.toString() : str;
return this;
},
toString: function() {
return this.items.join("");
}
};
a = new Buffer("one")
a.append("two")
console.log(a.toString());
// result: "onetwo"
Here is why you get such a behavior.
When following line is encountered
console.log(new Buffer("one").append(new Buffer("two")).toString());
let's breakdown it in smaller for our convenience.
browser evaluates new Buffer("one").
then browser evaluates new Buffer("two")
finally above two results are evalueated like result1.append(result2).
So when first gets evaluated Item array will have one. Then after second will be evaluated then item array will have two element "one" & "two" since it is shared array.
Consider first result as result1 and second result as result2. Now when third step is executed it will join result1's item array which is having two elements and result2's item array which is same as result1's so you get four element in log as onetwoonetwo.

Javascript - The quickest and most efficient way to replace object-based values in text

I have an object that looks like this:
var obj = {
a: "text",
b: "text 2",
c: "text 3",
...
}
and I have a bunch of strings that look like this:
var stringA = "http://{{a}}.something.com/",
stringB = "http://something.{{b}}.com/",
stringC = "http://something.com/{{c}}";
I want to replace {{(\w)}} with it's equivalent by going through obj and checking if it has my matched value for every string, but I'm sure there is a better and faster way to do so.
Any ideas?
Douglas Crockford wrote a function called supplant that does almost exactly what you want. I've altered the function slightly to match your double curly braces -
if (typeof String.prototype.supplant !== 'function') {
String.prototype.supplant = function (o) {
return this.replace(/{{([^{}]*)}}/g, function (a, b) {
var r = o[b];
return typeof r === 'string' ? r : a;
});
};
}
var obj = {
a: "text",
b: "text 2",
c: "text 3"
}
var stringA = "http://{{a}}.something.com/",
stringB = "http://something.{{b}}.com/",
stringC = "http://something.com/{{c}}";
alert(stringA.supplant(obj));
Demo - http://jsfiddle.net/saZGg/
var arr = /(.*){{(\w)}}(.*)/.exec(stringA);
arr[2] = obj[arr[2]];
arr.shift(); // remove full string at start
var newString = arr.join("");
Generally run a regular expression on the string,
execute and turn it into an array.
Swap one of the groups for the value in your object hash
join the array into your "compiled" string.
Or use one of the .replace solutions which is arguebly more elegant.
This should do it:
function format(str, values) {
return str.replace(/{{(\w)}}/g, function(match, name) {
return values[name] || match;
});
}
It just looks up whether the object has a property with the captured name. If not, nothing is replaced.
Usage:
var str = format(stringA, obj);

Categories