Благодаря adityajones мне удалось добраться туда :) Итак, хотя это мой правильный ответ, я буду отмечать его как правильный!
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/image" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="@drawable/gradient_image" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="6dp"
android:layout_marginBottom="18dp"
android:shadowColor="#000"
android:shadowRadius="7.0"
android:text="This is some random text"
android:textColor="@color/white"
android:textSize="22sp" />
</RelativeLayout>
Похоже, вам нужен способ представления различных объектов, так что идентичные пары ключ-значение (в разных порядках) могут быть распознаны как один и тот же ключ в map
. Один из вариантов - взять объект и sort
его записи в соответствии с алфавитным порядком клавиш, а затем stringify
отсортированные записи:
var m = new Map();
function opening(config) {
// ...
return "foo";
}
function open(config = {}) {
const key = objToKeyValStr(config);
if (!m.has(key)) {
m.set(key, opening(config));
console.log("open and set: ", config);
} else {
console.log("get from the map: ", config);
}
return m.get(config);
}
function replacer(_, value) {
if (typeof value !== 'object' || Array.isArray(value)) return value;
return objToKeyValStr(value);
}
const objToKeyValStr = obj => (
JSON.stringify(
Object.entries(obj).sort((a, b) => a[0].localeCompare(b[0])),
replacer
)
);
var c1 = { a: 1, b: 2 };
open(c1); // open and set [OK]
open(c1); // get from the map [OK]
open({ a: 1, b: 2 }); // get from the map
open({ b: 2, a: 1 }); // get from the map, even in different orders
open({ a: 1, b: 2, c: 3 }); // open and set
open({ a: 1, c: 3, b: 2 }); // get from the map
open({ a: 1, b: { c: 1, d: 1 }}); // open and set
open({ a: 1, b: { d: 1, c: 1 }}); // get from the map
{ a: 1, b: 2, c: {d: 4, e: 5} }
и{ a: 1, b: 2, c: {e: 5, d: 4} }
– hallaji 13 July 2018 в 06:23replacer
, переданной вJSON.stringify
, см. Edit - фактический ключ выглядит не очень хорошо, но я думаю, что это сработает, по крайней мере – CertainPerformance 13 July 2018 в 06:59