javascript-objects

Reducing global variables and maintaining function flow

巧了我就是萌 提交于 2020-01-15 15:33:57
问题 Javascript is not my first language of choice, nor is it one I am all that well versed in. Functional stuff fine, objects, not so much. I am looking to see if anyone can suggest options for dealing with the following scenario. function postcodeEntry(destination,postcode) { "use strict"; document.getElementById('googlemappostcode').innerHTML = <?php if ($_GET['page']==="fun") echo "<div id=\"map\"></div>' + ";?>'<form id="postcodeEntry" method="post" action="/" onsubmit="calcRoute(\'driving\'

Reducing global variables and maintaining function flow

房东的猫 提交于 2020-01-15 15:33:49
问题 Javascript is not my first language of choice, nor is it one I am all that well versed in. Functional stuff fine, objects, not so much. I am looking to see if anyone can suggest options for dealing with the following scenario. function postcodeEntry(destination,postcode) { "use strict"; document.getElementById('googlemappostcode').innerHTML = <?php if ($_GET['page']==="fun") echo "<div id=\"map\"></div>' + ";?>'<form id="postcodeEntry" method="post" action="/" onsubmit="calcRoute(\'driving\'

JSON Error when parsing “… has no method 'replace'”

痞子三分冷 提交于 2020-01-15 07:06:26
问题 Let me preface this with the admission that I am a complete programming and javascript noob and that fact is the source of my trouble. I'm trying to populate a large array of custom objects from a text file that I've saved to with json.stringify. When I grab the file contents and json.parse(them), I get the following error: var backSlashRemoved = text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@' ^ TypeError: Object (contents of file) has no method 'replace' The code that causes this

JSON Error when parsing “… has no method 'replace'”

南笙酒味 提交于 2020-01-15 07:06:20
问题 Let me preface this with the admission that I am a complete programming and javascript noob and that fact is the source of my trouble. I'm trying to populate a large array of custom objects from a text file that I've saved to with json.stringify. When I grab the file contents and json.parse(them), I get the following error: var backSlashRemoved = text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@' ^ TypeError: Object (contents of file) has no method 'replace' The code that causes this

JavaScript - Reduce Array of Objects by Key [duplicate]

别说谁变了你拦得住时间么 提交于 2020-01-14 12:15:53
问题 This question already has answers here : How to combine an array in javascript (3 answers) Group array of object nesting some of the keys with specific names (2 answers) Closed 2 years ago . Working with an array of objects like: const arr = [ {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2}, {name: "survey with select", value: "survey with select answer", count: 2}, {name: "werasd", value: "Donald", count: 1}, {name: "werasd", value: "Jim", count: 1} ]; I am trying to

JavaScript - Reduce Array of Objects by Key [duplicate]

梦想与她 提交于 2020-01-14 12:15:10
问题 This question already has answers here : How to combine an array in javascript (3 answers) Group array of object nesting some of the keys with specific names (2 answers) Closed 2 years ago . Working with an array of objects like: const arr = [ {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2}, {name: "survey with select", value: "survey with select answer", count: 2}, {name: "werasd", value: "Donald", count: 1}, {name: "werasd", value: "Jim", count: 1} ]; I am trying to

JavaScript - Reduce Array of Objects by Key [duplicate]

怎甘沉沦 提交于 2020-01-14 12:14:12
问题 This question already has answers here : How to combine an array in javascript (3 answers) Group array of object nesting some of the keys with specific names (2 answers) Closed 2 years ago . Working with an array of objects like: const arr = [ {name: "qewregf dqewafs", value: "qewregf dqewafs answer", count: 2}, {name: "survey with select", value: "survey with select answer", count: 2}, {name: "werasd", value: "Donald", count: 1}, {name: "werasd", value: "Jim", count: 1} ]; I am trying to

If an array is truthy in JavaScript, why doesn't it equal true?

丶灬走出姿态 提交于 2020-01-13 12:22:51
问题 I have the following code snippet: if([]) { console.log("first is true"); } The console says first is true which means [] is true. Now I am wondering why this: if([] == true) { console.log("second is true"); } and this: if([] === true) { console.log("third is true"); } are not true . If the console logged first is true in the first snippet, that means [] should be true, right? So why do the latter two comparisons fail? Here is the fiddle. 回答1: This is by specification. By the ECMAScript 2015

Can I alias a key in an object literal? [duplicate]

蓝咒 提交于 2020-01-13 10:22:46
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Self-references in object literal declarations I have an object literal which is used as a configuration element and is looked up for keys. customRendering:{ key1: func(){....}, key2: func(){....} } I have a situation where key2 and key3 can use the same function. Is there a way to assign the same function to both key2 and key3 without having to declare the function outside the object literal and without having

Can I alias a key in an object literal? [duplicate]

最后都变了- 提交于 2020-01-13 10:22:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Self-references in object literal declarations I have an object literal which is used as a configuration element and is looked up for keys. customRendering:{ key1: func(){....}, key2: func(){....} } I have a situation where key2 and key3 can use the same function. Is there a way to assign the same function to both key2 and key3 without having to declare the function outside the object literal and without having