使用映射和字符串模板文字来转换对象键值

时间:2019-10-13 10:56:12

标签: javascript object ecmascript-6

我有一个对象数组。我需要使用地图高阶函数转换数据。我应该使用prop函数助手进行转换。 prop函数将字符串作为参数。如何使用字符串模板文字将适当的回调传递给map:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "..."
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "..."
        testInstrumentationRunner "android.support.Singleton.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.firebase:firebase-core:17.2.0'
    testImplementation 'junit:junit:4.12'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50"

    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'us.belka:androidtoggleswitch:1.2.2'
    implementation 'commons-io:commons-io:2.4'
    implementation files('libs/mail.jar')
    implementation 'com.github.droidbond:LoadingButton:0.1.5'

    implementation 'com.samigehi:loadingview:1.1'

    implementation 'io.github.tonnyl:whatsnew:0.1.1'
    implementation 'com.github.GrenderG:Toasty:1.3.1'
    implementation 'com.lsjwzh:materialloadingprogressbar:0.5.8-RELEASE'
    implementation 'com.daimajia.swipelayout:library:1.2.0@aar'

    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'


    testImplementation 'junit:junit:4.12'
}

1 个答案:

答案 0 :(得分:3)

更改prop函数以同时接受keyobj。现在,您可以使用Array.map()调用它,然后将其传递给key函数。

const data = [{"age":25,"name":"Michael"},{"age":20,"name":"David"}]

const prop = key => obj => obj[key] // or String(obj[key]) if you want the value to always be a string

const name = data.map(prop('name')).join(',')
const age = data.map(prop('age')).join(',')

console.log(name) //Michael, David
console.log(age) // 25, 20