我如何使用console.logs这样的数组:
[Kzljszzz15OFcln1XGM, {make: "John Deere ", model: "5055", uid: "Kzljszzz15OFcln1XGM", year: "1953"}]
并将其转换为这个普通对象? :
{Kzljszzz15OFcln1XGM: {make: "John Deere ", model: "5055", uid: "Kzljszzz15OFcln1XGM", year: "1953"}
这是一个键值对,其中值是更多键值对?
到目前为止,我已经尝试过:
const myArray = //the array mentioned above
function toObject(arr) {
const rv = {};
for (let i = 0; i < arr.length; ++i) {
rv[i] = arr[i];
return rv;
}
console.log(toObject(myArray));
还有:
Object.assign({}, myArray);
这些甚至不会将myArray转换为普通对象,更不用说我出于某种原因所遵循的格式了。
我在几个完全错误的方向上设置了这个问题,因为我是从我的代码中的错误位置登录的。但现在我确定我问的是正确的问题。任何帮助,将不胜感激!
答案 0 :(得分:1)
如果要将索引作为键和奇数索引作为值, 你可以将数组转换为这样的对象:
function toObject(arr) {
const rv = {};
for (let i = 0; i < arr.length; i += 2) {
rv[arr[i]] = arr[i + 1];
}
return rv;
}
给出这样的数组:
var arr = ["Kzljszzz15OFcln1XGM", {make: "John Deere ", model: "5055", uid: "Kzljszzz15OFcln1XGM", year: "1953"}];
上述功能将给出:
{ Kzljszzz15OFcln1XGM:
{ make: 'John Deere ',
model: '5055',
uid: 'Kzljszzz15OFcln1XGM',
year: '1953' } }
答案 1 :(得分:0)
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (maxHeight != WITHOUT_MAX_HEIGHT_VALUE
&& heightSize > maxHeight) {
heightSize = maxHeight;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
getLayoutParams().height = heightSize;
} else {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec)
, getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
&#13;