javascript-objects

Is there any difference with using only location vs using window.location across browsers

天大地大妈咪最大 提交于 2019-12-17 19:46:43
问题 I find myself always writing: console.log(window.location.href); without even thinking about it. The majority of answers on SO also write it this way. Is there any reason why I can't just write: location.href since location is an object at window level? Are there any cross-browser compatibility issues with this? To Clarify: I know there is document.location - that is NOT what this question is about. This is about if there is any difference with using only location vs using window.location

Convert array of objects to array of arrays

谁说我不能喝 提交于 2019-12-17 16:59:56
问题 var json = [{one: "text1", two: "text2", three: 3, four: 4}, {one: "text3", two: "text4", three: 5, four: 6}, {one: "text5", two: "text7", three: 8, four: 9}] How can I convert the array of objects above into an array of arrays below? var array = [["text1", "text2", 3, 4], ["text3", "text4", 5, 6], ["text5", "text7", 8, 9]] Is there an ES2015 function to help convert it easily? If not a for loop might do. 回答1: You can use map and Object.values let array = json.map(obj => Object.values(obj));

How to convert an array of paths into JSON structure?

你说的曾经没有我的故事 提交于 2019-12-17 16:26:29
问题 I found the question How to convert a file path into treeview?, but I'm not sure how to get the desired result in JavaScript: I'm trying to turn an array of paths into a JSON tree: https://jsfiddle.net/tfkdagzv/16/ But my path is being overwritten. I'm trying to take something like this: [ '/org/openbmc/path1', '/org/openbmc/path2', ... ] ... and turn it into... output = { org: { openbmc: { path1: {}, path2: {} } } } I'm sure this is pretty easy, but I'm missing something. 回答1: const data = [

Why can't I set a JavaScript function's name property?

南楼画角 提交于 2019-12-17 14:14:36
问题 I am learning JavaScript and read that functions are like objects and can have properties set like this: var person = function(){ } person.name="John Smith"; //output "" person.age=21; //output 21 person.profession="Web Developer"; //output "Web Developer" Why is the name property blank? Thanks 回答1: Because name is a non-standard, non-writable property of function objects. Function declarations and named function expressions are named , while you have an anonymous function expression whose

Get the value of an object with an unknown single key in JS

狂风中的少年 提交于 2019-12-17 09:54:05
问题 How can I get the value of an object with an unknown single key? Example: var obj = {dbm: -45} I want to get the -45 value without knowing its key. I know that I can loop over the object keys (which is always one). for (var key in objects) { var value = objects[key]; } But I would like to know if there is a cleaner solution for this? 回答1: Object.keys might be a solution: Object.keys({ dbm: -45}); // ["dbm"] The differences between for-in and Object.keys is that Object.keys returns all own key

jQuery object to JavaScript object

房东的猫 提交于 2019-12-17 09:20:09
问题 If I have a textarea like var txtarea = $('textarea') , how can I set the value to it using the JavaScript property value , and not the jQuery property val() ? I think I need to convert txtarea to a JavaScript object, but how do I? 回答1: You can use the dereferencing operator, or the .get() method to "Retrieve the DOM elements matched by the jQuery object." Examples: txtArea[0].value = "something"; or: txtArea.get(0).value = "something"; 回答2: The jQuery .get() will do that for you http://api

Using curl POST with variables defined in bash script functions

巧了我就是萌 提交于 2019-12-17 05:21:11
问题 When I echo I get this, which runs when I enter it into the terminal curl -i \ -H "Accept: application/json" \ -H "Content-Type:application/json" \ -X POST --data '{"account":{"email":"akdgdtk@test.com","screenName":"akdgdtk","type":"NIKE","passwordSettings":{"password":"Starwars1","passwordConfirm":"Starwars1"}},"firstName":"Test","lastName":"User","middleName":"ObiWan","locale":"en_US","registrationSiteId":"520","receiveEmail":"false","dateOfBirth":"1984-12-25","mobileNumber":"9175555555",

Benefit of using bracket notation (with variables) to access a property of an object

杀马特。学长 韩版系。学妹 提交于 2019-12-16 18:04:34
问题 While utilizing alternatives such as the dot operator make sense when needing to access some stored information, I'm having a little bit of difficulty understanding why and in what scenarios we would use variables to accomplish the same task. For example: var myObj = { prop1: "val1", prop2: "val2" }; var prop1val = myObj.prop1; // val1 var prop2val = myObj.prop2; // val2 versus: var testObj = { 12: "Namath", 16: "Montana", 19: "Unitas" }; var playerNumber = 16; var player = testObj

Summarize count of occurrences in an array of objects with Array#reduce

天大地大妈咪最大 提交于 2019-12-14 04:19:06
问题 I want to summarize an array of objects and return the number of object occurrences in another array of objects. What is the best way to do this? From this var arrayOfSongs = [ {"title":"Blue","duration":161.71,"audioUrl":"/assets/music/blue","playing":false,"playedAt":"2016-12-21T22:58:55.203Z"}, {"title":"Blue","duration":161.71,"audioUrl":"/assets/music/blue","playing":false,"playedAt":"2016-12-21T22:58:55.203Z"}, {"title":"Blue","duration":161.71,"audioUrl":"/assets/music/blue","playing"

Iterate over Nested Objects in Array and delete the Property names and Values

我与影子孤独终老i 提交于 2019-12-14 03:34:50
问题 I have the array as below and what I want to do is to remove the Property names i.e keys and values from the below array based on the values in the other array. Options would be to create a new array as below with only values from the Keep Array or to remove the values which are not listed in the "keep" array from the original array. var Keep=["ItemID","ItemNumber","OptionNo","Quantity","Price","Description","StockStatus","Url"]; var originalarray=[ { "ItemID": 1, "ItemNumber": "611741",