我的后端返回如下数据。
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
如何将其转换为如下所示的Objectof对象
{BOF:true,CM:true, ...}
谢谢。
答案 0 :(得分:4)
您可以使用某些ES6语法,例如使用Object.assign
,扩展语法,计算出的属性,然后它实际上具有非常好的功能性编程风格:
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}];
const result = Object.assign(...permissions.map(
({PermCode, IsvalidPerm}) => ({[PermCode]: IsvalidPerm})
));
console.log(result);
答案 1 :(得分:2)
尽管您可以在 <android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/viewPagerGaleria"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/pageIndicatorView">
</android.support.v4.view.ViewPager>
//Forget about this, is just a indicator
<com.rd.PageIndicatorView
android:id="@+id/pageIndicatorView"
android:layout_width="wrap_content"
app:layout_constraintBottom_toTopOf="@id/viewPagerGaleria"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:piv_padding="50dp"
app:piv_animationType="scale"
app:piv_viewPager="@id/viewPagerGaleria"
android:layout_height="wrap_content"
/>
</android.support.constraint.ConstraintLayout>
的文档中找到答案(免责声明:它是Array
),但对于单线而言,语法有些棘手。
您在这里:
map
对于您的编辑,如果要创建对象,请使用const data = [
{"PermCode":"BOF","IsvalidPerm":true},
{"PermCode":"CM","IsvalidPerm":false},
{"PermCode":"CV","IsvalidPerm":true},
{"PermCode":"DAS","IsvalidPerm":true},
{"PermCode":"RPT","IsvalidPerm":true},
{"PermCode":"VM","IsvalidPerm":true}
];
const formatted = data.map(item => ({[item.PermCode]: item.IsvalidPerm}));
console.log(formatted);
:
reduce
答案 2 :(得分:0)
请始终在OP中显示您的尝试,以便我们了解您的移动方向。
您可以像下面那样使用“ Array.map”和“ Object.assign”
var permissions = [{"PermCode":"BOF","IsvalidPerm":true},{"PermCode":"CM","IsvalidPerm":false},{"PermCode":"CV","IsvalidPerm":true},{"PermCode":"DAS","IsvalidPerm":true},{"PermCode":"RPT","IsvalidPerm":true},{"PermCode":"VM","IsvalidPerm":true}]
let result = Object.assign(...permissions.map(d => ({ [d.PermCode]: d.IsvalidPerm})))
console.log(result)