How do I remove a key from an element object property - polymer

I have a Polymer 1.0 custom element which has a property called obj which is an object, say
obj = { a: 1,
b: 2}
How do I remove one of the keys?
I have tried this.set('obj.a', undefined)
and this.set('obj.a', null)
but result is {a: null (or undefined),
b: 2}
where what I want is just to remove 'a' leaving {b:2}
is there a correct way of doing it?

Use delete. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
Example:
let x = {a: 1, b: 2, '#sss': 3};
delete x.a; // x is now {b: 2, '#sss': 3}
delete x['#sss']; // x is now {b: 2}

Related

ES6 spread operator output

I have a very basic question regarding spread operator. When we do
let numbers = [1,2,3,4];
console.log(...numbers);
While it flattens out the list, is the output format actually a string like "1 2 3 4" OR are they still separate number list?
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
In your example you would have
let numbers = [1,2,3,4];
console.log(...numbers);
// This is the same
console.log(1, 2, 3, 4);
Your values are spreaded as seperate values out of the array. You can do this with functions as well:
const numbers = [1, 2];
const add = (a, b) => { return a + b; };
console.log(add(...numbers)); // -> add(1, 2) -> 3
const numbers = [1, 2, 3];
const [ firstNumber, ...restOfTheNumbers ] = numbers;
console.log(firstNumber, restOfTheNumbers); // 1 [ 2, 3 ]

Immutable mergeIn overwrites the original values

I've got a problem when using Immutable.js with nested map and list, I was expected mergeIn will merge the new value with the old one, however it overwrite the value instead.
How to get it correctly?
const state = fromJS({
list: []
})
.update("list", l => l.push({ a: 1 }))
.mergeIn(["list", 0], { b: 3 })
.getIn(["list", 0])
I was expecting the final value is { a: 1, b: 3 }, however, the actual output is { b: 3 }
Immutable.js is not supposed to merge plain objects like that (prior to v4 anyway).
state.mergeIn(["list", 0], { b: 3 })
is equivalent to
state.updateIn(["list", 0], m => m.merge({ b: 3 }))
When the updater function of .updateIn() is called, since { a : 1} has no merge() method in its prototype, Immutable.js doesn't merge it but replaces with the new object instead.
You can fix this if you just make an Immutable.Map out of {a : 1}. Or use Immutable.js v4+: apparently, it uses Immutable.merge() to merge plain objects, exactly the way you expected it to work.
I tested it with immutableJS v4.0.0-rc.12 and it works as you expected, i.e. { a: 1, b: 3 }. So maybe you just have to update to that version

SoapUI's Script Assertion is failing for the json response

I want to check if every instance of occupanysequenceorder matches the request inputted for this field. When I do a log.error it outputs this:
ERROR:[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
ERROR:1
So as the request inputted 1, it means in the list all instances needs to equal 1, which it does above. However when I perform an assert:
assert roominfo.occupancySequenceOrder.flatten() == occupancysequenceorder_request
It throws a false assertion and I am not sure why? How can I get the script assertion to pass with it performing the relevant check. I change assert to
assert roominfo.occupancySequenceOrder.flatten().contains(occupancysequenceorder_request) and it passes but I am not sure if that is actually does the correct check to ensure every instance of occupanysequenceorder is matching the request inputted.
Below is the code:
json.testregions.each { roominfo ->
log.error roominfo.occupancySequenceOrder.flatten()
log.error occupancysequenceorder_request
assert roominfo.occupancySequenceOrder.flatten() == occupancysequenceorder_request
}
Looking at OP's other question and its data from here
You can try below Script Assertion :
//Check if the response is not empty
assert context.response, "Response is empty or null"
//Modify the value of the quest or read it thru properties if you want
def requestValue = 1
def json = new groovy.json.JsonSlurper().parseText(context.response)
json.regions.each { region ->
region.hotels.each { hotel ->
hotel.roomInformation. each { room ->
assert room.occupancySequenceOrder == requestValue, "Value did not match for room ${room.hotelRoomId}"
}
}
}
Rather try:
roominfo.occupancySequenceOrder.every { it == 1 }
flatten() will have no effect on flat List.
You may also try:
roominfo.occupancySequenceOrder.unique() == [1]
if you'd like to compare lists.

Kotlin: Can you use named arguments for varargs?

For example, you might have function with a complicated signature and varargs:
fun complicated(easy: Boolean = false, hard: Boolean = true, vararg numbers: Int)
It would make sense that you should be able to call this function like so:
complicated(numbers = 1, 2, 3, 4, 5)
Unfortunately the compiler doesn't allow this.
Is it possible to use named arguments for varargs? Are there any clever workarounds?
To pass a named argument to a vararg parameter, use the spread operator:
complicated(numbers = *intArrayOf(1, 2, 3, 4, 5))
It can be worked around by moving optional arguments after the vararg:
fun complicated(vararg numbers: Int, easy: Boolean = false, hard: Boolean = true) = {}
Then it can be called like this:
complicated(1, 2, 3, 4, 5)
complicated(1, 2, 3, hard = true)
complicated(1, easy = true)
Note that trailing optional params need to be always passed with name.
This won't compile:
complicated(1, 2, 3, 4, true, true) // compile error
Another option is to spare vararg sugar for explicit array param:
fun complicated(easy: Boolean = false, hard: Boolean = true, numbers: IntArray) = {}
complicated(numbers = intArrayOf(1, 2, 3, 4, 5))
Kotlin Docs says clearly that:
Variable number of arguments (Varargs)
A parameter of a function (normally the last one) may be marked with
vararg modifier:
fun <T> asList(vararg ts: T): List<T> {
val result = ArrayList<T>()
for (t in ts) // ts is an Array
result.add(t)
return result
}
allowing a variable number of arguments to be passed to the function:
val list = asList(1, 2, 3)
Inside a function a vararg-parameter of type T is visible as an
array of T, i.e. the ts variable in the example above has type
Array<out T>.
Only one parameter may be marked as vararg. If a vararg parameter
is not the last one in the list, values for the following parameters
can be passed using the named argument syntax, or, if the parameter
has a function type, by passing a lambda outside parentheses.
When we call a vararg-function, we can pass arguments one-by-one,
e.g. asList(1, 2, 3), or, if we already have an array and want to
pass its contents to the function, we use the spread operator
(prefix the array with *):
val a = arrayOf(1, 2, 3)
val list = asList(-1, 0, *a, 4)
From: https://kotlinlang.org/docs/reference/functions.html#variable-number-of-arguments-varargs
To resume, you can make it using spread operator so it would look like:
complicated(numbers = *intArrayOf(1, 2, 3, 4, 5))
Hope it will help
The vararg parameter can be anywhere in the list of parameters. See example below of how it may be called with different set of parameters. BTW any call can also provide lambda after closed parenthesis.
fun varargs(
first: Double = 0.0,
second: String = "2nd",
vararg varargs: Int,
third: String = "3rd",
lambda: ()->Unit = {}
) {
...
}
fun main(args: Array<String>) {
val list = intArrayOf(1, 2, 3)
varargs(1.0, "...", *list, third="third")
varargs(1.0, "...", *list)
varargs(1.0, varargs= *list, third="third")
varargs(varargs= *list, third="third")
varargs(varargs= *list, third="third", second="...")
varargs(varargs= *list, second="...")
varargs(1.0, "...", 1, 2, 3, third="third")
varargs(1.0, "...", 1, 2, 3)
varargs(1.0)
varargs(1.0, "...", third="third")
varargs(1.0, third="third")
varargs(third="third")
}

Immutable.js - is it possible to insert element into list at arbitrary position?

How do I insert an element at arbitrary position of Immutable.js List?
You are looking for the splicemethod:
Splice returns a new indexed Iterable by replacing a region of this Iterable with new values.
splice(index: number, removeNum: number, ...values: any[])
Where you can specify the index and if you write 0 as removeNum it will just insert the values at the given position:
var list = Immutable.List([1,2,3,4]);
console.log(list.toJS()); //[1, 2, 3, 4]
var inserted = list.splice(2,0,100);
console.log(list.toJS()); //[1, 2, 3, 4]
console.log(inserted.toJS()); //[1, 2, 100, 3, 4]
Demo JSFiddle.
It is worth pointing out that you can also use the insert method which is in fact synonymous with list.splice(index, 0, value) but it feels more intuitive and improves readability greatly.
const myImmutableList = Immutable.fromJS(['foo', 'baz'])
const newList = myImmutableList.insert(1, 'bar')
console.log(newList.toJS()) //["foo", "bar", "baz"]