I am using the helper function turf.point()
const feature = turfHelpers.point(coords, properties, { id: properties.id });
properties looks like this
properties = {
id: 1,
thisWorks: 'no problem'
foo: {
thisDoesntWork: 'this is a problem'
}
}
When I create feature with turfHelpers.point(), it messes with the object. The nested object is not an object anymore, but gets stringyfied...
So, features.properties is
{
id: 1,
thisWorks: 'no problem'
foo: "{
thisDoesntWork: 'this is a problem'
}"
}
Now, I cannot access. feature.properties.foo.thisDoesntWork anymore, because its a string...
Why is turf.js doing that?
Let's put the question in the runnable form.
const turfHelpers = turf.helpers;
const coords = [100, 14];
const properties = {
id: 1,
thisWorks: 'no problem',
foo: {
thisDoesntWork: 'this is a problem'
}
};
var feature1 = turfHelpers.point(coords, properties, {
id: properties.id
});
// Print out on console
console.log(feature1.properties); //look OK
console.log(feature1.properties.foo); //also OK
console.log(feature1.properties.foo.thisDoesntWork); //also OK
<script src="https://cdnjs.cloudflare.com/ajax/libs/Turf.js/5.1.5/turf.min.js"></script>
Then, hopefully, it is helpful for discussion that leads to a solution.
Related
I have an array data but it declared with const. I am rather confused to change the data in array. Here is my array:
const person = {
name: "Person A", ---> the data I want to change
age: 100,
favDrinks: [
"coffee",
"temulawak", --->> the data I want to change
"tea"
],
greeting: function() {
console.log("Hello World"); --->> the data I want to change
}
}
How can I change the data of person.name, person.favDrinks[1], and greeting function of "Hello World". Is there a possible way to change constant data? I want to change to text strings. Here is my code :
function personName(nama ){
let person.name = nama;
person.push("name:" nama );
}
console.log(personName("Molly"));
But they are all wrong. Please help me. Thank you. :)
You are doing a couple of things wrong.
You are pushing into an object, instead of an array.
You are redefining the object person by using let inside your function.
You can change properties of a const object; however, you cannot reassign it.
Simply add return person
const person = {
name: "Person A",
age: 100,
favDrinks: ["coffee", "temulawak", "tea"],
greeting: function () {
console.log("Hello World");
},
};
function personName(nama) {
person.name = nama;
return person;
}
console.log(personName("Molly"));
This is what you'll get after your changes have been made:
{
name: 'Molly',
age: 100,
favDrinks: [ 'coffee', 'temulawak', 'tea' ],
greeting: [Function: greeting]
}
You are using let to create a variable with the name person.name which is not possible and then pushing it into person, in your case this is not what you should do, instead just change the values directly.
function personName(nama) {
person.name = name
return person
}
console.log(personName("Molly"));
if a constant variable is a string or a jumber, it cannot be changed. but if it is an array or object it cannot be changed but items in the object or array can be added, removed, or edited.
person is an object, not an array.
You can change properties values of a constant object.
Working Demo :
const person = {
name: "Person A",
age: 100,
favDrinks: [
"coffee",
"temulawak",
"tea"
],
greeting: function() {
console.log("Hello World");
}
};
person.name = "Person B";
person.favDrinks[1] = "Soft Drinks";
person.greeting = function() {
console.log('Hello Guys!');
}
console.log('updated object', person);
I am following " Learn javascript " on Scrimba and I got into a weird situation.
I learned that const variables can not be reassigned and there are methods in arrays and objects where you can add or remove elements from them (e.g arr.push())
const s = [5, 7, 2];
function editInPlace() {
"use strict";
s = [2, 5, 7];
}
editInPlace();
Console error:
Error: SyntaxError: unknown: "s" is read-only (/index.js:1)
But here comes the magic, when they assigned the const array variables new values with specific indexes then it worked well.
const s = [5, 7, 2];
function editInPlace() {
"use strict";
//s = [2, 5, 7];
s[0] = 2;
s[1] = 5;
s[2] = 7;
}
editInPlace();
console.log(s)
Console output:
[2, 5, 7]
Now I am not able to understand why is this happening.
Looking forward to listen from you.
I have the following object:
[ { id:
'/subscriptions/resourcegroups/coco-test/providers/microsoft.devtestlab/schedules/shutdown-computevm-runscripts2',
name: 'shutdown-computevm-runscripts2' },
{ id:
'/subscriptions/resourcegroups/coco-test/providers/microsoft.devtestlab/schedules/shutdown-computevm-packer',
name: 'shutdown-computevm-packer' } ]
I am trying to write a script that does something if it can't find a specific value in the name key of any of this object.
Example: shutdown-computevm-test
If there is no name anywhere in this object that matches this value, then I want my code to do something.
I'm new to nodejs, I tried things like includes(), indexOf etc, but these are either not the correct ways to do it or I never got the syntax right.
Any hints are appreciated.
Something like this should work for you;
const result = [ { id:
'/subscriptions/resourcegroups/coco-test/providers/microsoft.devtestlab/schedules/shutdown-computevm-runscripts2',
name: 'shutdown-computevm-runscripts2' },
{ id:
'/subscriptions/resourcegroups/coco-test/providers/microsoft.devtestlab/schedules/shutdown-computevm-packer',
name: 'shutdown-computevm-packer' } ];
const found = result.some((part) => part.name === 'shutdown-computevm-test');
if (! found) {
// do something here
}
I prefer it to filter as it won't wait to iterate over all items in the array and will shortcut as soon as it is found.
Use Array.prototype.find()
const arr = [ { id:
'/subscriptions/resourcegroups/coco-test/providers/microsoft.devtestlab/schedules/shutdown-computevm-runscripts2',
name: 'shutdown-computevm-runscripts2' },
{ id:
'/subscriptions/resourcegroups/coco-test/providers/microsoft.devtestlab/schedules/shutdown-computevm-packer',
name: 'shutdown-computevm-packer' } ]
let rs = arr.find(item => item.name === 'shutdown-computevm')
let rs2 = arr.find(item => item.name === 'shutdown-computevm-runscripts2')
console.log(rs) // undefined
console.log(rs2) // obj
I'm trying to create a script that will insert some values in to an object.
I basically want to end up with a series of objects that would look something like this -
myObj.data.person[0].name = 'name 1';
myObj.data.person[1].name = 'name 2';
myObj.data.person[2].name = 'name 3';
etc
Here is my object which contains an array of objects.
var addressBook = {
data: [
person = {
name: '',
address: ''
}
]
}
And a for loop to insert repeating information.
for (i=0; i < 10; i++)
{
myObj.data.person[i] = {name: 'Joe Bloggs', address: 'Main Street'};
console.log(myObj.data.person.name);
}
Whenever I run this code I get the following error -
Uncaught TypeError: Cannot set property 'person' of undefined at <anonymous>:14:24
So, the question is where am I going wrong ? And furthermore would this be considered the right way to go about creating a list of Objects (e.g. Person 1, Person 2 etc)?
(I'm ultimately thinking of how I can create something like a Person constructor and use a loop to create multiple Person objects).
Thanks,
Please try to change your object notation in following way
var addressBook = {
data: [{
name: '',
address: ''
},
{
name: 'Roshan',
address: ''
},
{
name: 'Roshan1',
address: ''
}
]
}
try this :
myObj.data[i].person = 'John';
myObj.data[i].address = 'NYC';
See this Answer as addendum to isetty ravitejakumar's Answer
You should consider writing a prototype / class for your usecase.
In this case you could keep better track of your data.
maybe something like this:
var AddressBook = {},
Person = {};
(function() {
AddressBook.prototype = {
data: [],
add: function(person) {
this.data.push(person);
}
};
Person = function(name, address) {
this.name = name;
this.address = address;
}
Person.prototype = {
name,
address
};
})();
I have set of json objects.
Languages
User Details
User Details have the languages field - which have more than 1 values.
Here is my sample json
$scope.languages = [
{id: 1, text: 'English'},
{id: 2, text: 'French'},
{id: 3, text: 'Hindi'},
{id: 4, text: 'Telugu'}
];
$scope.users = [{name: 'first user', status: 1,language:"1"},
{name: 'second user', status: 2,language:"1,2"},
{name: 'third user', status: 3,language:"1,3,4"}];
In my view i want to list the user name and languages.
<li ng-repeat="user in users">{{user.name}} - {{testString}}</li>
I know to do for single value. But for multiple values. I have the logic but i don't know how to implement. I am thinking that. First i have to split the user language string and change into array and then find the index of the language id and then return the language text.
I have the code to return language name from ID.
var foundItem = $filter('filter')($scope.languages, { id: 3 }, true)[0];
var index = $scope.languages.indexOf(foundItem );
$scope.result = $scope.languages[index].text;
So now the problem is how to print the languages next to the user name?
I tried like this
$scope.testString = function() {
return "return test string";
}
{{testString}}
But its not working. If this works we can pass the langugae codes as parameter and i can add the search code inside the testString function.
Thanks
testString is a function so you cannot use it like {{testString}}, you have to call that function {{testString()}}
You can simplify your code like this.
$scope.getLanguages = function (langs) {
var l = [];
angular.forEach(langs.split(','), function (lang) {
l.push(findLanguageTextById(parseInt(lang)));
})
return l.join(', ');
}
function findLanguageTextById (langId) {
for(var i = 0;i<$scope.languages.length;i++) {
if ($scope.languages[i].id == langId) {
return $scope.languages[i].text;
}
}
}
I have created a working demo for your problem take a look.
http://plnkr.co/edit/Cdl8y58IExoVSZV6lp76?p=preview
I think you are not calling the function,
$scope.testString = (function() {
return "return test string";
})();
I have an ObjectManager, which holds a reference to all objects that are created. The problem is that the ObjectManager is not referencing the object that was created, but instead it seems to be creating a new instance of it. Any help would be appreciated. Thanks!
var Fieldset = function (options) {
var fieldset = ($.extend(true, {
id: '',//Let's assume this has been overridden with 'MyFieldset' via the options param.
title: '',
accordion: '',
fields: [],
hidden: false,
Show: function () { $('#' + this.id).show() },
Hide: function () { $('#' + this.id).hide() }
}, options));
if (fieldset.id != null && fieldset.id != '')
ObjectManager.fieldsets[fieldset.id] = fieldset;//Save a reference to this object in the ObjectManager, so I can call ObjectManager.GetFieldset('MyFieldset'). A reference is only saved if an id is provided.
log(ObjectManager.GetFieldset(fieldset.id) == fieldset);//true
return fieldset;
}
Edit:
Sorry, I thought it was clear what I wanted this to do. There is nothing special about ObjectManger. It just has a property and Get method for each of my objects. For simplicity I only included the fieldsets property and Getter. I hope this clears up my intentions.
var ObjectManager =
{
fieldsets: {},
GetFieldset: function (id) { return this.fieldsets[id]; }
};
Edit2:
After some testing, I found something odd... Hopefully someone can explain to me why this is happening.
var myFieldset = new Fieldset({ id: 'MyFieldset' });
log(myFieldset == ObjectManager.GetFieldset('MyFieldset'));//I just found that it is true in this case...
//... However, this is not the normal way I create Fieldsets... This is:
var editForm = new Form({
dataStore: function () { return ClientsDS; },
id: 'ClientEditForm',
fieldSets: [
new Fieldset({
id: 'ClientDetailsFieldSet',
title: 'Details',
fields: [
new Field({ id: 'ClientID', name: 'ID', property: 'ID', fieldType: 'hidden', value: '0' })
]
})
]
});
log(editForm.fieldSets[0] == ObjectManager.GetFieldset('ClientDetailsFieldSet'));//false
On EDIT2:
Your objects are not equal, because they are not the same. The equality operator does not say these two objects have the same key/value pairs, they are equal when they are the same object.
For instance,
var a = b = {a: 1, b:2};
//This is b = {a: 1, b: 2}; a = b; In case you were wondering
a === b //true
var a = {a: 1, b: 2},
b = {a: 1, b: 2};
a === b //false
Hmm, your Fieldset constructor is returning an object. Perhaps try calling it as Fieldset({...}) instead of new Fieldset({...})?
I am assuming that your Form class looks something like your Fieldset class, i.e. that it $.extends (makes a deep copy) the options you give it with its internal "prototype". The object returned is the extended prototype not the options extended with the prototype object. Try changing the order of your $.extend arguments (put options second and the internal "prototype" third) and see if that changes anything. Alternatively, post your Form class :-)