Answers:
您几乎是正确的,只是放了一些引号。用正则引号将整个内容包装起来将按#demo + {this.state.id}字面值给您字符串-您需要指出哪些是变量,哪些是字符串文字。由于里面的任何内容{}都是内联JSX 表达式,因此您可以执行以下操作:
href={"#demo" + this.state.id}这将使用字符串文字#demo并将其连接到的值this.state.id。然后可以将其应用于所有字符串。考虑一下:
var text = "world";还有这个:
{"Hello " + text + " Andrew"}这将产生:
Hello world Andrew 您还可以将ES6字符串插值/ 模板文字与`(反引号)和${expr}(插值表达式)一起使用,这与您似乎想做的事情更接近:
href={`#demo${this.state.id}`}这将基本上替代的值this.state.id,并将其连接到#demo。等效于:"#demo" + this.state.id。
exampleData =
        const json1 = [
            {id: 1, test: 1},
            {id: 2, test: 2},
            {id: 3, test: 3},
            {id: 4, test: 4},
            {id: 5, test: 5}
        ];
        const json2 = [
            {id: 3, test: 6},
            {id: 4, test: 7},
            {id: 5, test: 8},
            {id: 6, test: 9},
            {id: 7, test: 10}
        ];example1 =
        const finalData1 = json1.concat(json2).reduce(function (index, obj) {
            index[obj.id] = Object.assign({}, obj, index[obj.id]);
            return index;
        }, []).filter(function (res, obj) {
            return obj;
        });example2 =
        let hashData = new Map();
        json1.concat(json2).forEach(function (obj) {
            hashData.set(obj.id, Object.assign(hashData.get(obj.id) || {}, obj))
        });
        const finalData2 = Array.from(hashData.values());我推荐第二个例子,它更快。